Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

BASH Scripting 1

Status
Not open for further replies.

spbr2

Technical User
Feb 15, 2017
5
GB
Hi,

I understand how to copy a specific line to a file, but is it possible to copy a specific line from file1.log and print it to line 2 within file2.log?
If so, how.

Thank you
 
Do you mean insert to line 2, or overwrite line 2?

This isn't pretty, but it should work (doing insert)...

Code:
# Putting line 3 of file1.log to second line of file2.log

LINE=3

( head -1 file2.log
  head -${LINE} file1.log | tail -1
  tail --lines=+2 file2.log
) > file2.new

mv file2.log file2.old
mv file2.new file2.log

 
If you want to replace line 2 in file2.log...

Code:
# Putting line 3 of file1.log to second line of file2.log

INSERT=2
REPLACE=3
WHICH=${REPLACE}

LINE=3

( head -1 file2.log
  head -${LINE} file1.log | tail -1
  tail --lines=+${WHICH} file2.log
) > file2.new

mv file2.log file2.old
mv file2.new file2.log


 
Thank you SamBones for the last 2 replies.
If you can, could you help me with the below:

I need to create a program in which it does the following:
- Select a word from file.log
- print it into /etc/network/services under wireless key
- ping the network
- - if successful save the successful ping to a file
- if not, then try the next word in the list

Huge Thank you

Thank you
 
Uh, no, this isn't a programming service.

Why don't you try writing it yourself and then post the code and any error messages you get. Post questions about specific problems you are having. Then we can help you work through the problems.

Doing it that way will help you learn how to do it yourself much faster than taking code that you don't understand from some stranger on the Internet.

 
Ok

Would a for loop be best suited for this job?

Thank you
 
Hi SamBones

Another Question with the below:
Bash:
#!/bin/bash
LINE=3
( head -1 file2.log
  head -${LINE} file1.log | tail -1
  echo "line"  >> -${LINE} file2.log
  tail --lines=+2 file2.log
) > file2.new

mv file2.log file2.old
mv file2.new file2.log

If I wanted to echo a line next to the word, how would that be done, as I've failed with the above.

Thank you
 
echo "line" >> file2.log

you may want to touch file2.log sometime before you append. Some versions of unix/Linux may not like it if you append to a non-existent file.

==================================
advanced cognitive capabilities and other marketing buzzwords explained with sarcastic simplicity


 
Hi johnherman,

Unfornualty thats not what im looking for, so I want to echo "line" to a specific line, e.g. line 2.

Thank you
 
This is not possible if the file is open for read/write, as you will not know where the file pointer is positioned. If (when) the file is closed, you can use awk. for this example, n = second line, t = text to insert, "line" in this case. Code is untested.

awk -v "n=2" -v "t=line" '(NR==n) { print t } 1' /path/to/file

==================================
advanced cognitive capabilities and other marketing buzzwords explained with sarcastic simplicity


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top