LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   strange SED question (https://www.linuxquestions.org/questions/linux-software-2/strange-sed-question-603785/)

pixellany 12-01-2007 01:55 PM

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?

bigrigdriver 12-01-2007 02:31 PM

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.

urticadioica 12-01-2007 02:33 PM

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.

pixellany 12-01-2007 04:06 PM

Quote:

Originally Posted by urticadioica (Post 2976893)
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

colucix 12-01-2007 05:03 PM

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


syg00 12-01-2007 05:24 PM

pixellany, go take your medications and lie down .... ;)

No-one (I mean no sane person) would do that.

pixellany 12-01-2007 07:36 PM

Quote:

Originally Posted by syg00 (Post 2977009)
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.....

Tinkster 12-01-2007 09:20 PM

Quote:

Originally Posted by pixellany (Post 2977099)
I will go back to using my mother's maiden name for the delimiter.....

Dot? Percent? Tilde? Caret? Slash? ... =8-}


Cheers,
Tink

colucix 12-02-2007 05:04 AM

Quote:

Originally Posted by Tinkster (Post 2977157)
Dot? Percent? Tilde? Caret? Slash? ... =8-}

Tilde was the name of an aunt of mine in Rome! ;)

Ciao :]


All times are GMT -5. The time now is 08:15 AM.