LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-11-2010, 10:21 AM   #1
disruptive
Member
 
Registered: Dec 2005
Posts: 76

Rep: Reputation: 15
replacement with sed


I have some text that I would like to replace, I want only to replace the values associated with the text, or I can replace the entire line. However I am finding that some of the reg expressions I am using do not seem to work.

Here is the line in the file:

@ xaxis label char size 3.03

I want to replace the numbers. How best to match? I can match and get the text string i.e. "@ xaxis label char size", but I want to replace the entire line. How best?

This is what I have now:

sed 's/^@ xaxis label char size/@ xaxis label char size 4.0/g' teststring.txt

I have tried putting some reg expressions to match the decial number as well as follows, but this does not work either.

sed 's/^@ xaxis label char size[0-9]*[,.]?[0-9]+*/ replacment text/g' teststring.txt

however this does not seem to work either. I appreciate matching the line is probably smart or is there a way to replace just the decimal?

Thanks.
 
Old 08-11-2010, 10:46 AM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,561

Rep: Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939
Well if we can assume that there will be no digits prior to the ones at the end, you could do:
Code:
echo "@ xaxis label char size 3.03" | sed -r 's/([^[0-9]+).*/\14.0/'
 
Old 08-11-2010, 10:58 AM   #3
disruptive
Member
 
Registered: Dec 2005
Posts: 76

Original Poster
Rep: Reputation: 15
Thanks, but I do need to match the proceeding text before replacing the numbers.
 
Old 08-11-2010, 11:07 AM   #4
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Do you need to match the preceding text exactly, or you just want to be able to have a back-reference to the text on the right hand side of the sed expression?

Anyhow, try this:

Code:
echo "@ xaxis label char size 3.03" | \
 sed 's/\(^.*\)\(@ xaxis label char size \)\([[:digit:]+]\).*/\1\24.0/'
The \1 references the start of the match, up UNTIL your string; the \2 references your string, and the rest, which assumes the number part is the end of the line, gets replaced.

Output:
Code:
sasha@reactor: echo "@ xaxis label char size 3.03" | \
 sed 's/\(^.*\)\(@ xaxis label char size \)\([[:digit:]+]\).*/\1\24.0/'
@ xaxis label char size 4.0

Last edited by GrapefruiTgirl; 08-11-2010 at 11:08 AM. Reason: broke long lines with backslash for readability.
 
Old 08-11-2010, 11:16 AM   #5
disruptive
Member
 
Registered: Dec 2005
Posts: 76

Original Poster
Rep: Reputation: 15
That works fine now, thanks!

Is there anymore that will help explain a little more about what you have done below, i.e. the brackets, what are they doing?




Quote:
Originally Posted by GrapefruiTgirl View Post
Do you need to match the preceding text exactly, or you just want to be able to have a back-reference to the text on the right hand side of the sed expression?

Anyhow, try this:

Code:
echo "@ xaxis label char size 3.03" | \
 sed 's/\(^.*\)\(@ xaxis label char size \)\([[:digit:]+]\).*/\1\24.0/'
The \1 references the start of the match, up UNTIL your string; the \2 references your string, and the rest, which assumes the number part is the end of the line, gets replaced.

Output:
Code:
sasha@reactor: echo "@ xaxis label char size 3.03" | \
 sed 's/\(^.*\)\(@ xaxis label char size \)\([[:digit:]+]\).*/\1\24.0/'
@ xaxis label char size 4.0
 
Old 08-11-2010, 11:20 AM   #6
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
With certain types of regexes (in sed, in this case, but awk too), expressions like \(blah\) can be referenced on the right hand side. Each time a \(blah\) is encountered, it can be re-printed in the output by referencing it with a \1 or \2 or \3 etc.. Each match of \(blah\) will be incrementally referenced. There may be a limit of 9 such matches I believe - you'd need to check the docs on that though.

The [[:digit:]+] is a regex match for a quantity of one or more digits.

Does this clear it up?

Last edited by GrapefruiTgirl; 08-11-2010 at 11:24 AM. Reason: added stuff in ( ... )
 
Old 08-11-2010, 11:23 AM   #7
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Plus - you could (and maybe should) tweak the end of the entire match, to properly account for the period and anything which might follow the decimal number(s), unless you're sure that the numbers part is the last thing on the line; otherwise, the end of the line following the number(s) will be lost.
 
Old 08-11-2010, 11:36 AM   #8
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Code:
echo "@ xaxis label char size 35.03" | \
 sed 's/\(^.*\)\(@ xaxis label char size \)\([[:digit:]+]\)\(.*\)/\1\24.0/'
Here's a slight improvement - now, if the digit(s) you are replacing happen to be more than a single digit (followed by anything), it will still work - but it still depends on the numbers bit being the last thing on the line.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Sed: perform replacement only on even-numbered lines? kmkocot Linux - Newbie 3 07-20-2010 01:27 PM
Sed pattern replacement icyrail Programming 3 10-27-2009 05:33 AM
Sed column replacement. keysorsoze Programming 11 12-01-2008 09:50 AM
replacement with sed DeepSeaNautilus Programming 6 10-01-2008 06:48 AM
Replacement with sed or awk DeepSeaNautilus Programming 4 08-05-2008 11:17 AM


All times are GMT -5. The time now is 08:25 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration