Bash, Perl, Sed How to write to line 10?

Up vote 2 down vote favorite share g+ share fb share tw.

I need a script that can write text to an exsisting file starting on line 10. It is a blank line so it won't be a find / replace. Would like preferably it to be in bash, but anything that the terminal can interpret will work just fine.

RE-EDITED: Sorry but still having a bit of a problem after I tested... Think it has something to do with what I want write to a file. Maybe this will make it easier.. 3 c 4 d 5 e 6 f 7 g 8 h 9 I 10 zone "$zone" in { 12 type master; 13 file "/etc/bind/db. $zone"; 14 }; 15 k 16 l 17 m Thanks in Advance, Joe perl bash sed awk link|improve this question edited Feb 5 '11 at 4:05 asked Feb 4 '11 at 0:56jmituzas12917 75% accept rate.

Use ed, of course. – tchrist Feb 4 '11 at 1:04.

Using sed: sed -i -e '10a\ new stuff' file Using bash: IFS=$'\n' line=0 while read -r line; do i=$((i+1)) if test $line -eq 10; then echo "new stuff" else echo "$line" fi done file. Tmp mv file. Tmp file Note that I'm not really sure if you mean insert at line 10 or at line 11, so double check the places I wrote 10 above.

You might want 9 for the sed command or 11 for the bash version. In perl, you can use the $NR variable. Open FILEHANDLE, ") { if ($NR == 10) { # do something } } And in awk, it's NR.

Awk 'NR! = 10 { print } NR == 10 { print "Something else" }' file But note that you can find and replace a blank line, e.g. Sed -i -e 's/^$/replacement text/' file.

Thanks I can see sed looks to be much easier... and yes insert at line 10 since there is already text on line 9 and 11. – jmituzas Feb 4 '11 at 1:09 sed -i is not standard. – tchrist Feb 4 '11 at 1:27 Sure.

It'll only work on Linux, FreeBSD, Mac OS, and Cygwin. On Solaris, AIX, HP-UX, etc. you'd need GNU sed installed, or to redirect output to a tempfile then mv it. – Mikel Feb 4 '11 at 1:32 Or s/sed/perl/ :) – tchrist Feb 4 '11 at 2:19 Exactly.

Perl -p -i -e .... – Mikel Feb 4 '11 at 2:20.

With sed: sed '10 s/^/text' file >file. New && mv file. New file With gnu-sed: sed -i '10 s/^/text' file With awk: awk 'NR==10 {$0="text"} 1' file >filen.

New && mv file. New file.

Sed -i is not standard. – tchrist Feb 4 '11 at 1:27 That's why I wrote "with gnu-sed" :) – marco Feb 4 '11 at 9:55.

You could just do: head -10 fileA > fileB; echo "new text" >> fileB; Note that this is fractionally quicker than doing it with sed.

Sed 10cFoobar foo replaces line 10 with "Foobar". This may contain line breaks: sed 4cFoobar"\nFoobar" foo This is in contrast to Mikel's solution who used a instead of c. The difference is that one replaces, the other one appends.

If the file already has 9 lines, it would be easy on the shell ;) $ echo "I am line 10" >> file-with-9-lines. Txt If the file may contain less than 9 lines or even may be larger (and you still want to write into line 10) the perl modul Tie::File will make things realy easy: #! /usr/bin/perl use Tie::File; tie my @file, 'Tie::File', '/path/to/some/file' or die $!

; $file9 = "I am the content of line 10.

– tchrist Feb 4 '11 at 2:19 @Mikel sorry I was having problems and erros so I re-edited... Thanks for so many quick responses. – jmituzas Feb 5 '11 at 4:09.

Ruby (1.9+) $ ruby -i. Backup -ne 'print ($. ==10)?

"word\n":$_' file.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions