PDA

View Full Version : writing string to specific line



aliahsan81
October 29th, 2009, 05:36 PM
I want to put a text string in file using any method on specific line


line number 30

text to be put %this is location /var/www/filename

Arndt
October 29th, 2009, 05:42 PM
I want to put a text string in file using any method on specific line


line number 30

text to be put %this is location /var/www/filename

You can use 'sed':


sed -i '4i\
hi' file


adds "hi" as line 4 in the file "file".

What should happen if there are less than 30 lines in the file?

aliahsan81
October 29th, 2009, 05:46 PM
If there is less then 30 line.

it will not add any thing because EOF character is reached.

Arndt
October 29th, 2009, 05:56 PM
If there is less then 30 line.

it will not add any thing because EOF character is reached.

The command I suggested will work, then.

ghostdog74
October 30th, 2009, 02:03 AM
I want to put a text string in file using any method on specific line


line number 30

text to be put %this is location /var/www/filename
see here example 2 (http://ghostdog74.livejournal.com/36703.html) for a simple example. You can modify to suit your need