[SOLVED] using the special RE codes \1,...,\9 in bash sed
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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.
Hopefully you are not color blind - I have colored the above. The red stuff is the first of two \(...\) I have used. The second refers to the remainder of the input line, after the match. So, on the RHS of the expression, \1 refers to the red stuff on the LHS, and \2 refers to the green stuff.
Note that this is just a simple example - play around a bit with it in your terminal to see what you can discover.
PS - Have a look at this page for a very good, more comprehensive (and more convoluted perhaps) instruction about back-references, and browse the remainder of that site for lots of good regex instruction:
Hopefully you are not color blind - I have colored the above. The red stuff is the first of two \(...\) I have used. The second refers to the remainder of the input line, after the match. So, on the RHS of the expression, \1 refers to the red stuff on the LHS, and \2 refers to the green stuff.
Note that this is just a simple example - play around a bit with it in your terminal to see what you can discover.
Oh bloody 'ell. Forgot the brackets haha. Thanks for reminding me that.
Hopefully you are not color blind - I have colored the above. The red stuff is the first of two \(...\) I have used. The second refers to the remainder of the input line, after the match. So, on the RHS of the expression, \1 refers to the red stuff on the LHS, and \2 refers to the green stuff.
Note that this is just a simple example - play around a bit with it in your terminal to see what you can discover.
Don't think it matters anymore but I think you should remove the backslash before the brackets in \(w.*1\) and \(.*\). Anyway thanks again
It seems to matter here, in this case. But, it will depend on the tool, the regex style, and the quoting in use, whether you need to escape, or not, or double-escape, certain things in regex's. I tend to always put escapes in situations like this as a habit - usually there's no harm. Maybe if I actually sat down and studied every possible combination of quotes and regex styles, I might really learn when to escape and when not to, but probably I won't invest the time. I'll just keep escaping things, and if testing poves it to be bad in a certain case, I fix it then.
It seems to matter here, in this case. But, it will depend on the tool, the regex style, and the quoting in use, whether you need to escape, or not, or double-escape, certain things in regex's. I tend to always put escapes in situations like this as a habit - usually there's no harm. Maybe if I actually sat down and studied every possible combination of quotes and regex styles, I might really learn when to escape and when not to, but probably I won't invest the time. I'll just keep escaping things, and if testing poves it to be bad in a certain case, I fix it then.
Code:
bash-4.1$ sed -r 's/ (s.*e) / 2\1 /g' test
Hey there. This is a 2sample text.
Testing *cough*... Very harsh testing don't.
bash-4.1$ sed -r 's/ \(s.*e\) / 2\1 /g' test
sed: -e expression #1, char 20: invalid reference \1 on `s' command's RHS
It seems to not to require \ before brackets here.
Code:
bash-4.1$ sed 's/([^a-zA-Z])/ \1 /g' test
sed: -e expression #1, char 20: invalid reference \1 on `s' command's RHS
bash-4.1$ sed 's/\([^a-zA-Z]\)/ \1 /g' test
Hey there . This is a sample text .
Testing * cough * . . . Very harsh testing don ' t .
It seems to require \ before brackets here.
Its probably has to do with the fact that I'm using [] in the second code. Well I'm a bit confused but I don't think its gonna be a big problem since I can just use trial and error method when needed. Anyway thank you all for helping me out.
Its probably has to do with the fact that I'm using [] in the second code. Well I'm a bit confused but I don't think its gonna be a big problem since I can just use trial and error method when needed. Anyway thank you all for helping me out.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.