LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 12-01-2007, 02:55 PM   #1
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 740Reputation: 740Reputation: 740Reputation: 740Reputation: 740Reputation: 740Reputation: 740
strange SED question


Composing this question, I somehow think of David Letterman and the "Stupid Pet Tricks"......

Helping someone on SED, I came up with a puzzle:

This works:

sed 's\old\new\g' filename


replaces old with new with "\" acting as the delimiter.

Now, how would one "escape" something inside the expression?
 
Old 12-01-2007, 03:31 PM   #2
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,908

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
A quick google search shows instances of double backslash to escape a character, as well as a few instances in which quadruple backslash is used.
 
Old 12-01-2007, 03:33 PM   #3
urticadioica
LQ Newbie
 
Registered: Oct 2005
Posts: 14

Rep: Reputation: 0
Can't say, since it depends which sed you're using. According to the standard, backslash is illegal as a delimiter in an s expression.
 
Old 12-01-2007, 05:06 PM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Original Poster
Rep: Reputation: 740Reputation: 740Reputation: 740Reputation: 740Reputation: 740Reputation: 740Reputation: 740
Quote:
Originally Posted by urticadioica View Post
Can't say, since it depends which sed you're using. According to the standard, backslash is illegal as a delimiter in an s expression.
OK, so BASH doesn't follow the standard.....or perhaps the standard is really saying proceed at your own risk.

Just adding more backslashes won't do it, because "s" simply goes from one to the next---eg:
sed 's\\\' means "replace the first occurrence of nothing with nothing
 
Old 12-01-2007, 06:03 PM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
I'd use the square brackets to match a string made of a single character. For example suppose you have a file containing a <tab>
Code:
>>> cat testfile
hello   pixellany

>>> # the following does not work (for the reason you explained)
>>> sed 's\\t\_\g' testfile
sed: -e expression #1, char 6: unknown option to `s'

>>> # this one works
>>> sed 's\[\t]\_\g' testfile
hello_pixellany
This permits for example to "escape" the square bracket itself, as in
Code:
>>> cat testfile
hello [

>>> sed 's\[[]\pixellany\g' testfile
hello pixellany

>>> # whereas the following (obviously) does not work
>>> sed 's\[\pixellany\g' testfile
sed: -e expression #1, char 15: unterminated `s' command
 
Old 12-01-2007, 06:24 PM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 18,496

Rep: Reputation: 3099Reputation: 3099Reputation: 3099Reputation: 3099Reputation: 3099Reputation: 3099Reputation: 3099Reputation: 3099Reputation: 3099Reputation: 3099Reputation: 3099
pixellany, go take your medications and lie down ....

No-one (I mean no sane person) would do that.
 
Old 12-01-2007, 08:36 PM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Original Poster
Rep: Reputation: 740Reputation: 740Reputation: 740Reputation: 740Reputation: 740Reputation: 740Reputation: 740
Quote:
Originally Posted by syg00 View Post
pixellany, go take your medications and lie down ....

No-one (I mean no sane person) would do that.
Sanity was not a requirement to sign on here.....

Seriously, you are of course right. But I (we) did learn something: BASH doesn't follow the standards + the post by colucix (Thank you)

I will go back to using my mother's maiden name for the delimiter.....
 
Old 12-01-2007, 10:20 PM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 913Reputation: 913Reputation: 913Reputation: 913Reputation: 913Reputation: 913Reputation: 913Reputation: 913
Quote:
Originally Posted by pixellany View Post
I will go back to using my mother's maiden name for the delimiter.....
Dot? Percent? Tilde? Caret? Slash? ... =8-}


Cheers,
Tink
 
Old 12-02-2007, 06:04 AM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
Quote:
Originally Posted by Tinkster View Post
Dot? Percent? Tilde? Caret? Slash? ... =8-}
Tilde was the name of an aunt of mine in Rome!

Ciao :]
 
  


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
Another SED question 3saul Linux - Software 2 01-28-2007 01:06 PM
sed question markhod Programming 4 08-03-2006 08:24 AM
[sed] "Advanced" sed question(s) G00fy Programming 2 03-20-2006 01:34 AM
strange sed/bash behavior mpdavig Programming 1 07-24-2004 03:27 AM
little sed question freelinuxcpp Linux - Software 3 01-20-2004 08:36 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:16 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