LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Replace line in file with two lines?! Possible? (https://www.linuxquestions.org/questions/linux-general-1/replace-line-in-file-with-two-lines-possible-481332/)

eur0dad 09-07-2006 01:38 PM

Replace line in file with two lines?! Possible?
 
I'm trying to replace a single line in a file with two lines. I.e:

....
AB = 12
...

to

....
AB = 12
CD = 34
....

Can this be done without reading in each line individually and then writing back to another file? I really want to avoid this method since it removes all tabbing and thus creates a horrid file with each line aligned to the far left of the page.

Thanks!

druuna 09-07-2006 02:05 PM

Hi,

I don't get this part:
Quote:

Can this be done without reading in each line individually and then writing back to another file? I really want to avoid this method since it removes all tabbing and thus creates a horrid file with each line aligned to the far left of the page.
This should work:

sed -i 's/AB = 12/AB = 12\nCD = 34/' infile

and the old fashion way also works:

sed 's/AB = 12/AB = 12\nCD = 34/' infile > outfile

This should not interfere with the layout of the file (this is also true for awk and other programs). You can manipulate those things, it's not done by default.

Hope this helps.

homey 09-07-2006 02:15 PM

Code:

sed -i.bak 's/AB = 12/&\nCD = 34/' file.txt

Nuts, too slow again.


All times are GMT -5. The time now is 09:43 PM.