为什么 sed 需要 3 个反斜杠才能使用常规反斜杠? [英] Why does sed require 3 backslashes for a regular backslash?

查看:48
本文介绍了为什么 sed 需要 3 个反斜杠才能使用常规反斜杠?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇,为什么 sed 需要 3 个 \ 来识别一个?我明白它需要 2 个,但我不需要 3 个.

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

这是我的 Windows 计算机上的示例,使用 Cygwin:

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

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

如果我不添加 3 个反斜杠,我会得到一个

If I don't add 3 backslashes, I get a

sed: -e expression #1, char 7: unterminated s' command

推荐答案

我能够使用 Vista 和 Cygwin 1.7.0 重现此行为.

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

  • 两个反斜杠产生错误
  • 三个四个反斜杠
  • 五个给出同样的错误

两个反斜杠在 shell 中变成一个反斜杠,然后在 sed 中转义作为中间分隔符的正斜杠.

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)

其中三个:前两个在 shell 中成为一个,然后在 sed 中转义第三个

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

\\\/ -> \\/

四:每一对在shell中变成一个单独的然后第一个结果在sed中转义第二个

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

\\\\/ -> \\/ 

哦,我忘了说单引号和双引号对我来说都是一样的(cmd.exe 没有像 Bash 等人所做的那样区分).

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).

这篇关于为什么 sed 需要 3 个反斜杠才能使用常规反斜杠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆