[SOLVED] replace any line in all files that contains "string <something>" with "string something else"
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
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 any line in all files that contains "string <something>" with "string something else"
I have a directory of files; each of which will have a line with some string "xxx" which may or may not be followed by some text e.g. "xxx a b c" or just "xxx". What I'd like to do is to replace these lines, regardless of whether they have text, with a specific string; which is something like "xxx 1 2 3".
There's probably something out there in internet land that does what I want, but I just wasn't able to understand it when I ran across it.
Thanks in advance for any suggestions not of the form "RTFM and learn how to use sed and regular expressions". =) I do this so infrequently that it's enough that know that it can be done and can actually do some of the simpler stuff from examples I already have.
I have a directory of files; each of which will have a line with some string "xxx" which may or may not be followed by some text e.g. "xxx a b c" or just "xxx". What I'd like to do is to replace these lines, regardless of whether they have text, with a specific string; which is something like "xxx 1 2 3".
There's probably something out there in internet land that does what I want, but I just wasn't able to understand it when I ran across it.
Thanks in advance for any suggestions not of the form "RTFM and learn how to use sed and regular expressions". =) I do this so infrequently that it's enough that know that it can be done and can actually do some of the simpler stuff from examples I already have.
Then you will be happy to know that this CAN be done with MANY tools, including but not,limited to SED, GSAR, PERL, Python, PHP, Pascal, C, C++, and possibly even advanced shell scripting (bash perhaps).
I have a directory of files; each of which will have a line with some string "xxx" which may or may not be followed by some text e.g. "xxx a b c" or just "xxx". What I'd like to do is to replace these lines, regardless of whether they have text, with a specific string; which is something like "xxx 1 2 3".
There's probably something out there in internet land that does what I want, but I just wasn't able to understand it when I ran across it.
Thanks in advance for any suggestions not of the form "RTFM and learn how to use sed and regular expressions". =) I do this so infrequently that it's enough that know that it can be done and can actually do some of the simpler stuff from examples I already have.
Is "xxx" necessarily in the beginning of a line?
If yes, you need this regex:
/^xxx.*$
If not, this one:
/xxx.*$
Things you want to understand in these regexes, one symbol per line
^
. (or \., it depends on the tool or way you write the regex, it may need to be escaped)
* (or \*, it depends on the tool or way you write the regex, it may need to be escaped)
$
After you can find what you need, you use some "()" or "\(\)" to remember the parts you need and put it in the replacement expression with something like:
s/^\(xxx\).*$/\1 1 2 3/ge
I have made a test file:
Code:
qweqewqeqw xxx lkjlklkj
xxx oiuoiuoiu
xxx
lasjkd as lkadsj k jalsd ja
aslkd a lkadsj dlaj dlask jda
a xxx
xxx
Note the fourth line has a space in the end!
And the replacement command with sed, as I could imagine you want. See these lines of my terminal, showing the commands I used (lines starting with "$") and their output (other lines).
Code:
$ cat xxx
qweqewqeqw xxx lkjlklkj
xxx oiuoiuoiu
xxx
lasjkd as lkadsj k jalsd ja
aslkd a lkadsj dlaj dlask jda
a xxx
xxx
$sed 's/^\(xxx\).*$/\1 1 2 3/g' xxx
qweqewqeqw xxx lkjlklkj
xxx 1 2 3
xxx 1 2 3
lasjkd as lkadsj k jalsd ja
aslkd a lkadsj dlaj dlask jda
a xxx
xxx 1 2 3
$
Thanks! I'll see what I can do with this and get back to you. Yes, the string is at the beginning of the line. Well, it might follow a tab or some blanks, but that's a mere detail.
What is the backslash 1? I think I can figure out the rest.
OK, I'm marking it as solved. Your example worked perfectly. I created a simple shell script as below, and it changed the files exactly as needed. Thanks again!
Code:
#!/bin/sh
for file in abc/*.def
do
sed -i 's/\(something\).*$/\1 \/etc\/somewhere\/someval/g' $file
done
But there is something missed here: the spaces you may have before the "xxx" must be written! "^" Is the imaginary place before the first thing in the line (include spaces!). To have optional spaces at the start of lines, use something like this:
Code:
$ sed 's/^\s*\(xxx\).*$/\1 1 2 3/g' filename
\s is any kind of space: spaces, tabs, newlines, ...
\1 means to write "the first thing we asked to remember". In this command, we have put the "xxx" among the special parenthesis, so that is what we are asking to repear. Put another \1 after the 2 and you will see it clearly.
Read a fairly small (but not too much) regex tutorial. It should give you many more ideas. And do not forget to learn about the special parts I have pointed in my first post.
Hi. As it turned out, there wasn't anything in front of the string I needed to change. As far as I can tell from spot checking the script I made changed over 1600 files to the correct value, including some that had already been manually set. After the spot check, I made those the active files, and everything seems to be OK.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.