LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 09-12-2014, 04:35 PM   #1
juergen852
LQ Newbie
 
Registered: Sep 2014
Posts: 5

Rep: Reputation: Disabled
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.

Any Ideas?
 
Old 09-12-2014, 05:05 PM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Quote:
Originally Posted by juergen852 View Post

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.
I don't get it, you want to replace the lines or the version number?
 
Old 09-12-2014, 05:22 PM   #3
juergen852
LQ Newbie
 
Registered: Sep 2014
Posts: 5

Original Poster
Rep: Reputation: Disabled
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.
 
Old 09-12-2014, 05:27 PM   #4
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195
Quote:
Originally Posted by juergen852 View Post
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
 
Old 09-12-2014, 05:38 PM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,128

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
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.
 
Old 09-12-2014, 05:40 PM   #6
juergen852
LQ Newbie
 
Registered: Sep 2014
Posts: 5

Original Poster
Rep: Reputation: Disabled
.... Maybe I'm just too old
Great Idea. Thanks.

I will try tomorrow and update here.
 
Old 09-21-2014, 01:45 PM   #7
juergen852
LQ Newbie
 
Registered: Sep 2014
Posts: 5

Original Poster
Rep: Reputation: Disabled
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?
 
Old 09-21-2014, 01:56 PM   #8
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195
Quote:
Originally Posted by juergen852 View Post
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.

Last edited by astrogeek; 09-21-2014 at 02:09 PM.
 
1 members found this post helpful.
Old 09-21-2014, 04:21 PM   #9
juergen852
LQ Newbie
 
Registered: Sep 2014
Posts: 5

Original Poster
Rep: Reputation: Disabled
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.
 
Old 09-21-2014, 04:54 PM   #10
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195
Quote:
Originally Posted by juergen852 View Post
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...

Anyway, glad to help, good luck!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to find and replace a text spanning multiple lines with sed haveanother Linux - Newbie 7 02-26-2012 06:29 AM
using sed to replace text on one line in a text file vo1pwf Linux - Newbie 5 06-24-2009 07:54 AM
[SOLVED] edit multiple lines of a text file into 1 line: schneidz Programming 2 04-09-2009 11:22 AM
How to replace text in multiple files bpk Linux - Newbie 2 02-10-2004 02:03 PM
trying to search and replace text file for single & multiple line breaks separately brokenfeet Programming 7 08-29-2003 01:56 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration