[SOLVED] how can backslash be executed as a metacharacter within single quotes?
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.
how can backslash be executed as a metacharacter within single quotes?
Hello everybody
I am reading the e-book "the Linux command line " by William Shotts Jr.
In page 309, he writes :
" since sed, by default, accepts only basic regular expressions, several of the characters in
our regular expression will be taken as literals, rather than as metacharacters. We can
solve both these problems with a liberal application of backslashes to escape the
offending characters:
sed 's/\([0-9]\{2\}\)\/\([0-9]\{2\}\)\/\([0-9]\{4\}\)$/\3-\1-\2/' distros.txt "
Now there are 2 things I cannot understand
1) how are the backslashes executed as escape characters since they are within single quotes ?
2) How does the application of backslashes solve the problem that sed doesnot accept ERE ?
Any help would really be appreciable
how are the backslashes executed as escape characters since they are within single quotes ?
The single quotes tell your shell that it should not process any escape sequences in the string. If you replace sed with e.g echo, you can see what sed actually receives as a parameter.
Quote:
Originally Posted by joham34
How does the application of backslashes solve the problem that sed doesnot accept BRE ?
Sed what?!
GNU sed supports POSIX BREs fine; \([0-9]\{2\}\)\/\([0-9]\{2\}\)\/\([0-9]\{4\}\)$ is a perfectly ordinary basic regular expression.
If you meant ERE, then the GNU sed manual tells why in the Extended regexps chapter: EREs and BREs differ only in that certain characters must be escaped. In other words, if you escape all ?, +, (, {, ) and } characters in an ERE, you get a BRE.
The single quotes tell your shell that it should not process any escape sequences in the string. If you replace sed with e.g echo, you can see what sed actually receives as a parameter.
Sed what?!
GNU sed supports POSIX BREs fine; \([0-9]\{2\}\)\/\([0-9]\{2\}\)\/\([0-9]\{4\}\)$ is a perfectly ordinary basic regular expression.
If you meant ERE, then the GNU sed manual tells why in the Extended regexps chapter: EREs and BREs differ only in that certain characters must be escaped. In other words, if you escape all ?, +, (, {, ) and } characters in an ERE, you get a BRE.
Thanks, I meant ERE indeed , I will correct it in my post . I didnt know that escaping converts these ERE metacharacters in BRE metacharacters.
I am completely covered and will mark the thread as solved
Thanks, I meant ERE indeed , I will correct it in my post . I didnt know that escaping converts these ERE metacharacters in BRE metacharacters.
I am completely covered and will mark the thread as solved
You're welcome. Technically, there are some additional differences between POSIX BREs and EREs, covered well in the Wikipedia POSIX Extended Regular Expressions article, but they do not apply to GNU sed.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.