Linux - Newbie This 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.
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.
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.
|
|
12-01-2007, 06:28 AM
|
#1
|
Member
Registered: Mar 2004
Location: Atlanta
Distribution: Redhat 9.0
Posts: 49
Rep:
|
sed (stream editor) problem
Dear friends,
I have the following problem with many curly braces.
In some file I have many patterns like this: {\{}}
I want to get rid of all of them
My question is: how can I do this with sed?
What is the correct regular expression, that matches this pattern?
I tried the following
sed -e 's/{\{}}//g' ... does no work
sed -e 's/{\\\{}}//g' ... does not work
sed -e 's/\{\\\{\}\}//g' ... does not work
So, how to do this?
Thanks.
Last edited by igor.R; 12-01-2007 at 06:32 AM.
|
|
|
12-01-2007, 06:48 AM
|
#2
|
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,251
|
Try
Code:
sed -e 's:{\\{}}::g'
Different separator char is generally a good idea.
|
|
|
12-01-2007, 07:01 AM
|
#3
|
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
For regular sed, only the backslash is a meta-character, and needs to be escaped.
sed 's/{\\{}}//g' file >modified-file
If you enable extended regular expressions then the { and } characters are meta-characters and need to be escaped as well.
sed -r 's/\{\\\{\}\}//g' file >modified-file
Last edited by jschiwal; 12-01-2007 at 07:03 AM.
|
|
|
12-01-2007, 07:15 AM
|
#4
|
Member
Registered: Mar 2004
Location: Atlanta
Distribution: Redhat 9.0
Posts: 49
Original Poster
Rep:
|
Quote:
Originally Posted by syg00
Try
Code:
sed -e 's:{\\{}}::g'
Different separator char is generally a good idea.
|
thank you very much
|
|
|
12-01-2007, 08:14 AM
|
#5
|
Member
Registered: Mar 2004
Location: Atlanta
Distribution: Redhat 9.0
Posts: 49
Original Poster
Rep:
|
Quote:
Originally Posted by jschiwal
For regular sed, only the backslash is a meta-character, and needs to be escaped.
sed 's/{\\{}}//g' file >modified-file
If you enable extended regular expressions then the { and } characters are meta-characters and need to be escaped as well.
sed -r 's/\{\\\{\}\}//g' file >modified-file
|
I still do not understand something about these regular expressions
with meta-characters.
Here is another problem:
how to transform
\text\{eps\} into \epsilon
I tried
sed -r -e "s/\\\text\\\{eps\\\}/\\\epsilon/g"
and
sed -r -e "s:\\\text\\\{eps\\\}:\\\epsilon:g"
but it does not work. What is wrong?
Last edited by igor.R; 12-01-2007 at 08:16 AM.
|
|
|
12-01-2007, 08:49 AM
|
#6
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
In the context of sed "\" has a special meaning and therefor must be escaped (with "\"--which makes it a bit confusing.) to be taken literally.
In context, however, "{" or "}" do not have special meaning and therefor do not need to be escaped. If you turn on extended Regexes, then they DO need to be escaped....
To replace "\text\" with text:
sed 's_\\text\\_text_g'**
To replace "{eps}" with "eps":
sed 's_{eps}_eps_g'
OR:
sed -r 's_\{eps\}_eps_g'
Any character which has a special meaning--in the context in which it used--must be escaped if it is to be taken literally.
In some cases, escaping is used to give special meaning when the character would other wise be taken literally.
Escaping an escape means that it is no longer an escape--thus you cannot use another escape to nullify the first escape. e.g. "\\\x" means literal "\", followed by an escaped "x". "\\\\" means 2 literal "\"s
say the above 5 times fast.....
**Remember that the first character after s is always the delimiter. Use any character you want to make it readable and to avoid ambiguity.
Last edited by pixellany; 12-01-2007 at 09:53 AM.
Reason: fixed boo-boo (-e should be -r)
|
|
|
12-01-2007, 09:18 AM
|
#7
|
Member
Registered: Mar 2004
Location: Atlanta
Distribution: Redhat 9.0
Posts: 49
Original Poster
Rep:
|
Quote:
Originally Posted by pixellany
In the context of sed "\" has a special meaning and therefor must be escaped (with "\"--which makes it a bit confusing.) to be taken literally.
In context, however, "{" or "}" do not have special meaning and therefor do not need to be escaped. If you turn on extended Regexes, then they DO need to be escaped....
To replace "\text\" with text:
sed 's_\\text\\_text_g'**
To replace "{eps}" with "eps":
sed 's_{eps}_eps_g'
OR:
sed -e 's_\{eps\}_eps_g'
Any character which has a special meaning--in the context in which it used--must be escaped if it is to be taken literally.
In some cases, escaping is used to give special meaning when the character would other wise be taken literally.
Escaping an escape means that it is no longer an escape--thus you cannot use another escape to nullify the first escape. e.g. "\\\x" means literal "\", followed by an escaped "x". "\\\\" means 2 literal "\"s
say the above 5 times fast.....
**Remember that the first character after s is always the delimiter. Use any character you want to make it readable and to avoid ambiguity.
|
Yes, I knew that, but I still do not understand what to do
if \ and { are next to each other in the pattern.
You may have misunderstood me.
I know how to replace {eps} with eps,
but to replace \{eps\} with eps is a different story
now I have both "\" and "{" next to each other and the first
makes it difficult to escape the second
sed -r "s/\\{eps\\}/eps/g"
or
sed -r "s/\\\{eps\\\}/eps/g"
do not work
|
|
|
12-01-2007, 09:52 AM
|
#8
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
First--I did a major boo-boo earlier---to use extended regexes, its sed -r, not -e.
in basic regexes, you don't need to escape the "{" or "}"
To replace "\{eps\}":
Basic:
sed 's_\\{eps\\}_newtext_g' (no -r)
Extended:
sed -r 's_\\\{eps\\\}_newtext_g'
This works on my system---what exactly happens when you try it?
Last edited by pixellany; 12-01-2007 at 10:27 AM.
Reason: left out the s
|
|
|
12-01-2007, 10:09 AM
|
#9
|
Member
Registered: Mar 2004
Location: Atlanta
Distribution: Redhat 9.0
Posts: 49
Original Poster
Rep:
|
Quote:
Originally Posted by pixellany
First--I did a major boo-boo earlier---to use extended regexes, its sed -r, not -e.
in basic regexes, you don't need to escape the "{" or "}"
To replace "\{eps\}":
Basic:
sed '_\\{eps\\}_newtext_g' (no -r)
Extended:
sed -r '_\\\{eps\\\}_newtext_g'
This works on my system---what exactly happens when you try it?
|
thank you.
It works.
It is interesting that it did not work
with / and : separators, it works with _ only.
Thanks again.
but what if one needs to replace \{phi0\} with phi_0 ?
Last edited by igor.R; 12-01-2007 at 10:16 AM.
|
|
|
12-01-2007, 10:25 AM
|
#10
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
Even with the "s" omitted? (yes, I screwed up again....)
Both of these work on my system:
sed 's/\\{eps\\}/newtext/g'
sed 's:\\{eps\\}:newtext:g'
For the truly obsessive, here's a good one:
sed 's\text\newtext\g' works...... Now, having used "\" for the delimiter, how would you escape something??
Last edited by pixellany; 12-01-2007 at 10:29 AM.
|
|
|
12-01-2007, 04:53 PM
|
#11
|
Member
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735
Rep:
|
Hi.
Quote:
Originally Posted by igor.R
... It is interesting that it did not work
with / and : separators, it works with _ only.
|
It worked for me using single quotes (and correcting the syntax):
Quote:
There are three quoting mechanisms: the escape character, single
quotes, and double quotes.
A non-quoted backslash (\) is the escape character. It preserves the
literal value of the next character that follows, with the exception of
<newline>. If a \<newline> pair appears, and the backslash is not
itself quoted, the \<newline> is treated as a line continuation (that
is, it is removed from the input stream and effectively ignored).
Enclosing characters in single quotes preserves the literal value of
each character within the quotes. A single quote may not occur between
single quotes, even when preceded by a backslash.
Enclosing characters in double quotes preserves the literal value of
all characters within the quotes, with the exception of $, `, and \.
The characters $ and ` retain their special meaning within double
quotes. The backslash retains its special meaning only when followed
by one of the following characters: $, `, ", \, or <newline>. A double
quote may be quoted within double quotes by preceding it with a
backslash.
-- man bash
|
Best wishes ... cheers, makyo
|
|
|
12-01-2007, 10:44 PM
|
#12
|
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
I hope you don't make a habit of using a backslash for a delimiter. It is the character used to escape other characters. You are confusing yourself with a contrived example.
|
|
|
All times are GMT -5. The time now is 04:25 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
|
|