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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
08-11-2010, 10:21 AM
|
#1
|
|
Member
Registered: Dec 2005
Posts: 76
Rep:
|
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.
|
|
|
|
08-11-2010, 10:46 AM
|
#2
|
|
Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 6,326
|
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/'
|
|
|
|
08-11-2010, 10:58 AM
|
#3
|
|
Member
Registered: Dec 2005
Posts: 76
Original Poster
Rep:
|
Thanks, but I do need to match the proceeding text before replacing the numbers.
|
|
|
|
08-11-2010, 11:07 AM
|
#4
|
|
Guru
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594
|
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.
|
|
|
|
08-11-2010, 11:16 AM
|
#5
|
|
Member
Registered: Dec 2005
Posts: 76
Original Poster
Rep:
|
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
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
|
|
|
|
|
08-11-2010, 11:20 AM
|
#6
|
|
Guru
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594
|
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 ( ... )
|
|
|
|
08-11-2010, 11:23 AM
|
#7
|
|
Guru
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594
|
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.
|
|
|
|
08-11-2010, 11:36 AM
|
#8
|
|
Guru
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594
|
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.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 11:59 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|