LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 05-06-2011, 12:16 PM   #1
joham34
Member
 
Registered: Oct 2009
Location: Greece
Distribution: ubuntu 14.04
Posts: 75

Rep: Reputation: 3
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

Last edited by joham34; 05-06-2011 at 01:26 PM.
 
Old 05-06-2011, 12:21 PM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,564
Blog Entries: 1

Rep: Reputation: 1024Reputation: 1024Reputation: 1024Reputation: 1024Reputation: 1024Reputation: 1024Reputation: 1024Reputation: 1024
1. I may be wrong, but single/double quoting only affects the possibility of variable substitution, NOT character escaping.
 
Old 05-06-2011, 01:06 PM   #3
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942
Quote:
Originally Posted by joham34 View Post
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 View Post
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.
 
Old 05-06-2011, 01:25 PM   #4
joham34
Member
 
Registered: Oct 2009
Location: Greece
Distribution: ubuntu 14.04
Posts: 75

Original Poster
Rep: Reputation: 3
Quote:
Originally Posted by Nominal Animal View Post
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
 
Old 05-06-2011, 01:52 PM   #5
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942
Quote:
Originally Posted by joham34 View Post
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.
 
  


Reply

Tags
regular expression, sed


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
X: Unable to open display '' (two single quotes) tgp1994 Debian 7 06-12-2010 12:12 AM
xargs removes single quotes after awk Poki Linux - Newbie 4 06-26-2009 09:25 AM
Variables within single quotes are not evaluated? Akhran Linux - Newbie 1 03-10-2006 08:32 PM
Using single quotes vs double quotes in PHP strings vharishankar Programming 6 07-11-2005 11:41 AM
replacing single quotes in file names adenardo Linux - General 2 05-27-2005 11:29 AM


All times are GMT -5. The time now is 09:10 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration