shell script: insert line in a file
I want to append to line number 45 of a file. Currently I am doing echo "something" >> file which puts "something" at the end of the file. But I would like to echo to, say, line number 33.
Thanks for any help. |
Quote:
what you need to do is separate the file into parts, output part 1 to a file, then the line to insert, then part 2 to the file. head and tail can help, i.e. "head -n 10 myfile" will print out the first 10 lines of "myfile". "tail -n 10 myfile" will print out the last 10 lines of "myfile". "wc -l myfile" will count the number of lines in "myfile". Hopefully that's enough to get you going. ;) |
You could use awk - something like:
awk '{if(NR == 45) {print "new line" >>"myfile"}}' (Not tested) |
Quote:
|
Quote:
|
another way
Code:
# awk 'NR==45{print "newline"}1' file > outfile |
sed way:
Code:
sed '45i\newline' file Also the backslash isn't necessary, I just put it there for clarity. Code:
sed '45inewline' file |
All times are GMT -5. The time now is 10:37 AM. |