How to identify a line and replace another string on that line using Shell script?
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.
How to identify a line and replace another string on that line using Shell script?
Folks,
I have a requirement similar to 'search and replace' but a little different. Basically I need to write a shell script that looks for a pattern/keyword to identify lines to be modified in a file, and then search/replaces another string on that line.
Say for example, I have the following definition in a file:
Here is an example:
cat filename | sed '/th/s/a/b/g' > newfilename
This searches filename and--for every line containing "th"--replaces all occurences of "a" with "b". It then writes the data to a new file: "newfilename".
the more general syntax is <address>, followed by the "s" command (substitute), with a "g" to make it global for the line---ie to change all instances.
I would suggest some time spent reading the tutorial mentioned above might be worthwhile.
There are (at least) a couple of ways of doing what you want in a single pass.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.