LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
LinkBack Search this Thread
Old 09-07-2006, 01:38 PM   #1
eur0dad
Member
 
Registered: Jul 2006
Posts: 57

Rep: Reputation: 15
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!
 
Old 09-07-2006, 02:05 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
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.
 
Old 09-07-2006, 02:15 PM   #3
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
Code:
sed -i.bak 's/AB = 12/&\nCD = 34/' file.txt

Nuts, too slow again.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SED - replace text in file on specific line 3saul Linux - Software 1 03-04-2006 07:01 PM
How can I use a shell script to add and replace lines in a file? abefroman Programming 10 12-27-2005 05:05 PM
Perl - Tpl file - Need to replace new line character. knnirmal Programming 2 09-07-2004 02:27 PM
merge multiple lines of a single file into one line groverrajiv Linux - Newbie 4 05-26-2004 02:38 AM
Using diff to compare file with common lines, but at different line numbers jimieee Linux - Newbie 3 05-10-2004 07:26 AM


All times are GMT -5. The time now is 05:41 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration