[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: sed substitution that contains forward slash

Craig White wrote:
On Tue, 2009-03-31 at 20:16 -0400, Todd Zullinger wrote:
Craig White wrote:
subtitle...fun with sed

I have a list of changes to make to a file...

dc              rc
-------------   -------
15T6145V        DELETED
NATL19502       DELETED
Q1500T3/CL120   DELETED

and things work until I get to the 3rd item which has a forward
slash and it fails to substitute with commands like below...

sed -i "s%${dc}%${rc}%g" ARsalesorderdetails.csv
sed -i "s/${dc}/${rc}/g" ARsalesorderdetails.csv

The latter producing error on screen...
sed: -e expression #1, char 17: unknown option to `s'

While the former simply doesn't complain but doesn't make the change
Hmm, it should work using a delimeter other than '/' or escaping the
'/'.  Escaping the '/' is a bit more of a pain than just changing
delimeters, as long as you know that whatever delimeter you pick won't
be in the strings you are substituting.

It seems to work for me:

$ dc="Q10MR11/FL12V"; rc=DELETED; echo "dc=${dc}"; echo "rc=${rc}"; \
  echo "This is some Q10MR11/FL12V text." | sed "s%${dc}%${rc}%g"
This is some DELETED text.
Interesting...using the vertical bar as the regex separator worked where
the % failed for me.

I never hit this before, but like another poster, I always use "#" unless it conflicts in which case I use "@" or "|" instead. I don't see how this would be an issue *unless* you have jobs running in the background of this shell.

Remember that "%" is semi-special to the shell, if you start a background job, you can kill it with
  kill %1
or whatever number shows up from the "jobs" command. However, it shouldn't do that unless you are using a built-in command like "fg" or "kill" and I suspect that is the only place that %number sequence is special.

If you get an answer let us know, I was unable to replicate your problem, although I doubt I use the "-i" option once in a decade with sed. I sort of remembered it was there, but had to look to check operation (it takes a suffix). If you can still get the % to fail, just for grins try putting -e before the command.

Bill Davidsen <davidsen tmr com>
  "We have more to fear from the bungling of the incompetent than from
the machinations of the wicked."  - from Slashdot

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]