LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   replacement with sed (https://www.linuxquestions.org/questions/programming-9/replacement-with-sed-825566/)

disruptive 08-11-2010 10:21 AM

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.

grail 08-11-2010 10:46 AM

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/'

disruptive 08-11-2010 10:58 AM

Thanks, but I do need to match the proceeding text before replacing the numbers.

GrapefruiTgirl 08-11-2010 11:07 AM

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


disruptive 08-11-2010 11:16 AM

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 (Post 4063113)
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



GrapefruiTgirl 08-11-2010 11:20 AM

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? :)

GrapefruiTgirl 08-11-2010 11:23 AM

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.

GrapefruiTgirl 08-11-2010 11:36 AM

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.


All times are GMT -5. The time now is 06:02 PM.