[SOLVED] How to replace single word in a file, with a series of lines?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
"A series of lines" is simply a string of characters with "newline sequences" in them ... which on any given system could be "CR," "LF," "CR+LF," or "LF+CR." (CR = carriage-return; LF = linefeed.) All of which can be represented by backslash-escape sequences.
Not really an awk person, but have used sed. So give that a try. Basically you issue sed, plus a command, and the file name. The output of the sed result ends up normally going to stdout, which is the terminal you're typing in. That's also a pretty good way to test and verify that things work the way you want. Then you can next recall the correct command and add a redirect "> new-filename" and a good idea is to not just replace the original file even though you can do that. Just make a new version or put a same named output file into a sub-directory so as to not risk some problem killing your original until you've assuredly got it right. And example sed command might be something like below where I have a file abc.txt containing the text 123 and I wish to change that text to be 456:
Code:
~/testcode$ cat abc.txt
123
~/testcode$ sed s/123/456/g abc.txt
456
So get trying to write the command and if you have trouble, post what you've tried and describe where you're stuck. Please review notes on how to use code tags.
This is the way the world ends
This is the way the world ends
This is the way the world ends
Not with a bang but a whimper.
... and this Insert file ...
Code:
Insert line 1, the first of 3.
Insert line 2, the middle child.
Insert line 3, the final line.
... this code ...
Code:
BashVar="~"$(paste -sd~ $Insert)"~"
sed "s/\bway\b/$BashVar/g" $InFile |tr '~' '\n' >$OutFile
... produced this OutFile ...
Code:
This is the
Insert line 1, the first of 3.
Insert line 2, the middle child.
Insert line 3, the final line.
the world ends
This is the
Insert line 1, the first of 3.
Insert line 2, the middle child.
Insert line 3, the final line.
the world ends
This is the
Insert line 1, the first of 3.
Insert line 2, the middle child.
Insert line 3, the final line.
the world ends
Not with a bang but a whimper.
This is the
Insert line 1, the first of 3.
Insert line 2, the middle child.
Insert line 3, the final line.
the world ends
This is the
Insert line 1, the first of 3.
Insert line 2, the middle child.
Insert line 3, the final line.
the world ends
This is the
Insert line 1, the first of 3.
Insert line 2, the middle child.
Insert line 3, the final line.
the world ends
Not with a bang but a whimper.
Daniel B. Martin
Last edited by danielbmartin; 03-29-2015 at 07:39 PM.
Reason: Tightened code
Grail gave you good advice, you just need to follow it:
Code:
# but your data has '/' in it so you could use
sed 's#find#replace#' file
I concur with that recommendation too.
Quote:
Originally Posted by Darrell22
Any other ideas, such as scripts?
The issue here is that scripts would be using the same commands you'd use on the command line, so if you can't attain results which work for you via the command line, you pretty much won't be able to do it via a script. We understand that changing your awk and sed may not be options, however your versions aren't that old. Why don't you try what you were recommended to do and please post the attempt and results within [code][/code] tags.
Splish, splash
I was takin' a bath
Long about a Saturday night... yeah!
Rub-a-dub
Just relaxin' in the tub
</body>
Thinkin' everything was all right.
... and this Insert file ...
Code:
Insert line 1, the first of 3.
Insert line 2, the middle child.
Insert line 3, the final line.
... this code ...
Code:
BashVar="~"$(paste -sd~ $Insert)"~"
sed "s#</body>#$BashVar#g" $InFile |tr '~' '\n' >$OutFile
... produced this OutFile ...
Code:
Splish, splash
I was takin' a bath
Long about a Saturday night... yeah!
Rub-a-dub
Just relaxin' in the tub
Insert line 1, the first of 3.
Insert line 2, the middle child.
Insert line 3, the final line.
Thinkin' everything was all right.
Daniel B. Martin
---------- Post added 03-30-15 at 10:19 AM ----------
With this InFile ...
Code:
Splish, splash
I was takin' a bath
Long about a Saturday night... yeah!
Rub-a-dub
Just relaxin' in the tub
</body>
Thinkin' everything was all right.
... and this Insert file ...
Code:
Insert line 1, the first of 3.
Insert line 2, the middle child.
Insert line 3, the final line.
Splish, splash
I was takin' a bath
Long about a Saturday night... yeah!
Rub-a-dub
Just relaxin' in the tub
Insert line 1, the first of 3.
Insert line 2, the middle child.
Insert line 3, the final line.
Thinkin' everything was all right.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.