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. |
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/' |
Thanks, but I do need to match the proceeding text before replacing the numbers.
|
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" | \ Output: Code:
sasha@reactor: echo "@ xaxis label char size 3.03" | \ |
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:
|
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? :) |
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.
|
Code:
echo "@ xaxis label char size 35.03" | \ |
All times are GMT -5. The time now is 06:02 PM. |