LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 12-01-2007, 06:28 AM   #1
igor.R
Member
 
Registered: Mar 2004
Location: Atlanta
Distribution: Redhat 9.0
Posts: 49

Rep: Reputation: 16
Unhappy 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.
 
Old 12-01-2007, 06:48 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 15,721

Rep: Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120
Try
Code:
sed -e 's:{\\{}}::g'
Different separator char is generally a good idea.
 
Old 12-01-2007, 07:01 AM   #3
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671
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.
 
Old 12-01-2007, 07:15 AM   #4
igor.R
Member
 
Registered: Mar 2004
Location: Atlanta
Distribution: Redhat 9.0
Posts: 49

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by syg00 View Post
Try
Code:
sed -e 's:{\\{}}::g'
Different separator char is generally a good idea.
thank you very much
 
Old 12-01-2007, 08:14 AM   #5
igor.R
Member
 
Registered: Mar 2004
Location: Atlanta
Distribution: Redhat 9.0
Posts: 49

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by jschiwal View Post
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.
 
Old 12-01-2007, 08:49 AM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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)
 
Old 12-01-2007, 09:18 AM   #7
igor.R
Member
 
Registered: Mar 2004
Location: Atlanta
Distribution: Redhat 9.0
Posts: 49

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by pixellany View Post
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
 
Old 12-01-2007, 09:52 AM   #8
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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
 
Old 12-01-2007, 10:09 AM   #9
igor.R
Member
 
Registered: Mar 2004
Location: Atlanta
Distribution: Redhat 9.0
Posts: 49

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by pixellany View Post
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.
 
Old 12-01-2007, 10:25 AM   #10
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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.
 
Old 12-01-2007, 04:53 PM   #11
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 729

Rep: Reputation: 75
Hi.
Quote:
Originally Posted by igor.R View Post
... 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
 
Old 12-01-2007, 10:44 PM   #12
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671
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.
 
  


Reply


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
SED Editor or not? RIG12 Programming 6 06-27-2007 12:25 PM
Howto transcode & relay a MPEG stream to a WMV stream?? crazyivan Linux - Software 0 06-15-2007 03:18 AM
mms stream problem zupermanz Linux - Newbie 9 02-20-2007 12:29 AM
mplayer-plugin problem with ONE stream Sammael Linux - Software 4 08-12-2006 09:44 AM
problem with stream krock923 Programming 1 02-26-2006 12:06 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:36 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration