0

Say I have want to match the string foo.. My regex would be foo\., to escape the special meaning of the . character.

Now say that in my sed command, I use the . as a delimiter.

For a search like, say, cat, this works fine.

echo cat | sed 's.cat.dog.g'

But when I have a literal . in my search string, I run into a problem. I would think, that since the . is my delimiter, my regex foo\. should become foo\\\.. Ideally, the first pass of unescaping would be done by sed to make this into foo\., and then this would get passed along to the regular expression engine.

But instead, it matches cat\\\.

echo 'cat.' | sed 's.cat\\\..dog.g' # cat.
echo 'cat\.' | sed 's.cat\\\..dog.g' # dog

Is there any way to make this work with a literal . when . is used as a delimiter?

I'm asking this question because I want to be able to have a function

function escapeForExtendedSed ($str, $delimiter = "/") { ... }

which can escape correctly depending on what the caller is using as a delimiter.

Chris Middleton
  • 5,654
  • 5
  • 31
  • 68
  • Your function signature doesn't look like a BASH function. – anubhava Jul 14 '15 at 19:17
  • Similar to csv files I think you'd need to use quotes or some other secondary quote like delimiter to specify to any parser when you your literal character should not be used as a delimiter. Otherwise that special character is ambiguous. – mba12 Jul 14 '15 at 19:17
  • @anubhava The function signature is in PHP (I'm doing an `exec` from PHP, for reasons that aren't really relevant to the question). – Chris Middleton Jul 14 '15 at 19:32
  • Does the `sedstr` function from [this answer](http://stackoverflow.com/a/29626460/258523) help here by obviating the need for alternate delimiters? – Etan Reisner Jul 14 '15 at 19:33
  • @Etan I don't actually need alternative delimiters - I just use an escaping function usually myself (and in PHP, I'd just use strtr or something like that) - but I was just curious if there was any way to do it. – Chris Middleton Jul 14 '15 at 19:44

1 Answers1

1

This works for me:

echo 'cat.' | sed 's.cat[\.].dog.g' # cat.
gymbrall
  • 2,063
  • 18
  • 21
  • That's matching `cat` followed by any character, not a period specifically. Try replacing `'cat.'` with `'cat@'` in the above to see what I mean. – Chris Middleton Jul 14 '15 at 19:31
  • 1
    Brilliant thinking. And I came up with one that works for left bracket as delimiter based off of your answer, but you need the `-E` flag to make it work: `echo 'cat[' | sed -E 's[cat(\[)[dog[g'`. EDIT: Or you can use `\(` and `\)` without the E flag, I forgot. – Chris Middleton Jul 14 '15 at 19:47