ProgrammingThis 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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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?
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.
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?
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.
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 ( ... )
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.