tags:

views:

455

answers:

5

I'm curious, why does sed need 3 \ just to recognize one? I'd understand it needing 2, but 3 I don't.

EDIT: here's an example on my Windows computer, using Cygwin:

echo "sample_input\whatever" | sed "s/\\\/\//"

If I don't add 3 backslashes, I get a sed: -e expression #1, char 7: unterminated s' command.

+2  A: 

Please show an example of what you have in future. in sed, say you want to replace a "\" with pipe (|), for example

$ cat file
asklfja \ asf

$ sed 's/\\/|/g' file
asklfja | asf

$ sed 's%\\%|%g' file #using different delimiter
asklfja | asf

you just need to escape it once.

Edit: To @OP's example, since you are using cmd.exe and not bash/ksh, cmd.exe doesn't like single quotes. I cannot produce your scenario. This works for my GNU sed on windows using 2 slashes

eg

C:\test>echo "sample_input\whatever" | sed "s/\\/\//"
"sample_input/whatever"
ghostdog74
@ghostdog74, I posted an example.
Geo
@geo, see my edit.
ghostdog74
I'm not using bash, ghostdog74, and with single quotes, I get the same error.
Geo
so in cygwin what shell are you using? you must be using a shell to run your commands.
ghostdog74
Right now, I'm just using cygwin binaries, from a Windows command prompt.
Geo
ok, i could not produce your error. works fine for me with GNU sed and cmd.exe. hope somebody with cygwin can help you.
ghostdog74
A: 

I guess, you assuming \\\n or \\\t as three backslashes there, but actually, its 2 backslashes and another patterns

   backslash          \\
   newline            \n
   tab                \t

also, / might need to escape because in s/.../, / is using for open and close parts.

so /\\\/\// would be \\ + \/ + \/ according to your updated example

S.Mark
+3  A: 

Your shell (probably bash) is doing its own escaping, and that's confusing you. You can use an echo command to see what is being passed, or it's easy to write a custom program (commonly named "showargs" or similar):

$ echo "s/\\\/\//"
s/\\/\//
$ echo "s/\\/\//"
s/\/\//

You can also use single quotes, which are treated differently in bash.

Roger Pate
I'm using Windows's `cmd.exe`, not bash.
Geo
@Geo: I guessed bash because you mentioned cygwin, but other shells also do escaping too.
Roger Pate
+1  A: 

I was able to reproduce this behavior using Vista and Cygwin 1.7.0.

  • Two backslashes produce the error
  • either three or four backslashes work
  • Five gives the same error

Two backslashes become a single backslash in the shell which then in sed escapes the forward slash which is the middle delimiter.

\\/ -> \/ (which makes the forward slash a regular character instead of a delimiter)

Three of them: The first two become one in the shell which then escape the third one in sed

\\\/ -> \\/

Four: Each pair become single ones in the shell then the first resulting one escapes the second in sed

\\\\/ -> \\/ 

Edit:

Oh, I forgot to say that both single quotes and double quotes worked the same for me (cmd.exe doesn't make the distinction that Bash, et al, makes).

Dennis Williamson
A: 

In my version of CYGWIN, it works as the original poster says, but, works differently (normally) if I use single quotes.

$ echo "sample_input\whatever" | sed 's/\\/\//'
sample_input/whatever
$ echo "sample_input\whatever" | sed "s/\\/\//"
sed: -e expression #1, char 7: unterminated `s' command

Hmmm..

James K