Replace text of unknown content with other text in file
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.
Replace text of unknown content with other text in file
I need a script that will replace text which I don't know ahead of time on a line that contains words that don't change ("foobar") and are unique to the file. Specifically, I want to make the line that reads:
Code:
foobar = 123abc \
into:
foobar = xyz456 \
but I don't know the exact contents of "123abc" ahead of time.
I'll also need to do this with a block of text as well, replacing the block of text between the lines containing "foo" and "bar" with a block of text from a file. Ex:
foo
Text To
Be Replaced
bar
becomes:
foo
Replacement
Text
bar
but, again, I don't know the exact contents of "Text To Be Replaced" ahead of time.
Now, we can combine these last two commands to replace a block of text in a file with the contents of another file. We do it like this:
sed -e '/START/r bar' -e '/START/,/END/d' foo
This finds a line containing START, deletes through to a line containing END, then reads in the contents of the file bar. Because the r command doesn't read in the file until the next input line is read, the d command is executed before the new text is read in, so the d command doesn't delete the new text, as one might expect, looking at this command. The -e option tells sed that the next argument is a command, rather than an input file. Although it is optional when there is only one command, if we have multiple commands, they must each be preceded with -e.
Not as clear as sed though, but that comment about
Quote:
so the d command doesn't delete the new text, as one might expect, looking at this command
tells me maybe I don't like very much sed for a good reason. I admit that the above command is not very clear, but for someone used with flex, it's not that bad.(and it does what one might expect )
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.