[SOLVED] Replace text lines identified by leading text in line within multiple files
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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 lines identified by leading text in line within multiple files
Hello,
I have some 100 files with different entries like
File1: text <span class="vers">V2.0</span> at line 22
File2: text <span class="vers">V2.20</span> at line 14
File3: text <span class="vers">V2.042</span> at line 29
--> at different line positions within those files.
I want to replace those different "vers">V2.xxx numbers with a unique entry like "vers">V2.2
I am well aware of sed -i.bak 's/foo/bar/g' * but this does not work as I need to replace the line, not only some text.
Problem: Some 100 .html-files which all contain some version information. This version information is not always the same number yet. So I want to replace former version information within those files with a new (same) one for all files.
Well, I cannot do a sed s/<span class="vers">V2.0</span>/<span class="vers">V2.3</span>/g' as the search argument is not always V2.0. Some of the files contain V2.1 some contain V2.03 or so.
And I want to replace that version number within all files with the same number.
So my idea was:
Search all files for the line that starts with <span class="vers">, and replace the former content of the whole text line with some new content.
Last edited by juergen852; 09-12-2014 at 05:27 PM.
Well, I cannot do a sed s/<span class="vers">V2.0</span>/<span class="vers">V2.3</span>/g' as the search argument is not always V2.0. Some of the files contain V2.1 some contain V2.03 or so.
And I want to replace that version number within all files with the same number.
So my idea was:
Search all files for the line that starts with <span class="vers">, and replace the former content of the whole text line with some new content.
Why do you think you cannot do that with sed?
Try this (test on an example file first - this is untested!):
Code:
sed -i 's/"vers">V2.[0-9]*/"vers">V2.2/g' filename
You can adjust the amount of the line that you match and replace as actually needed, this example is based on your own.
Last edited by astrogeek; 09-12-2014 at 05:33 PM.
Reason: tpos,typs,typos - and a better example
I've found using "*" like that can lead to unexpected matches (on zero repetitions) - better to use "+" if the data are known to always be composed of at least one digit.
OK, this helped me out, although I had to use the command 10 times with different Search-Terms.
There was "2.1.0" or "Beta 2.2" and so on who are not covered by a "V2.[0-9]*"
So back to my original Idea: Is there a way to replace a complete line of text with a new line of text if the search term ( <span class="vers") matches the text in the line at any position?
OK, this helped me out, although I had to use the command 10 times with different Search-Terms.
There was "2.1.0" or "Beta 2.2" and so on who are not covered by a "V2.[0-9]*"
So back to my original Idea: Is there a way to replace a complete line of text with a new line of text if the search term ( <span class="vers") matches the text in the line at any position?
How about just adapting the sed expression to handle those cases...
Code:
sed -i 's/"vers">V2.[0-9.]*/"vers">V2.2/g' filename
OR
sed -i 's/"vers">V2[^<]*/"vers">V2.2/g' filename
OR
sed -i 's/"vers">[^<]*/"vers">V2.2/g' filename
Your original examples did not include any versions with multiple decimals so the sed we gave did not handle them. Now it will.
The last one will cover absolutely anything between the angle-brackets (your Beta case).
*** EDIT ***
By the way, I have not intended to ignore your question about replacing the whole line, but you are always way ahead by being as specific as possible in doing replacements across multiple files. Replacing whole lines can easily corrupt the files if it encounters any number of special cases not anticipated by the expression. So always best to identify the range of cases then craft an expression that handles them.
sed -i 's/"vers">[^<]*/"vers">V2.2/g' filename solved my problem.
(I still wonder whether sed or awk may to the complete line replacement. Just in case I hit a problem which cannot be solved by expressions.)
Big Thanks to everybody who helped me.
You are welcome!
The following one (untested, but should be OK) is one way to do the full line...
Code:
sed -i 's/^<span class="vers">.*$/<span class="vers">V2.2<\/span>/' filename
... but I can think of several surprises that you might hit such as if there were any cases of additional text on a line you would miss the line or lose the additional text, leading spaces would cause a line to be missed, line-breaking whitespace would break it, etc...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.