bash 如何使用sed用“\/”替换路径字符串上的“/”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13971113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 04:03:45  来源:igfitidea点击:

how to replace "/" on path string with "\/" using sed?

bashshellsedsh

提问by qiushuitian

I've tried this:

我试过这个:

PATH="/user/dj/a/mydir"
PATH_FORMAT=`echo "${PATH}" | sed 's/\//\\//'`

but it only replace the first "/". I want the resulting PATH_FORMAT value to be:

但它只替换第一个“/”。我希望得到的 PATH_FORMAT 值是:

"\/user\/dj\/a\/mydir"

How can I do that?

我怎样才能做到这一点?

回答by Thor

Add a gflag to your substitute command:

g为您的替代命令添加一个标志:

echo "${PATH}" | sed 's/\//\//g'

Or more readable, as per Jonathan Wakelyin the comments:

或者更具可读性,正如Jonathan Wakely在评论中所说:

echo "${PATH}" | sed 's:/:\/:g'

To achieve what you describe in your question, you need one more backslash:

要实现您在问题中描述的内容,您还需要一个反斜杠:

echo /user/dj/a/mydir | sed 's:/:\\/:g'

Output:

输出:

\/user\/dj\/a\/mydir

回答by blubase

Use Shell's parameter expansion, if you are running a shell script:

如果您正在运行 shell 脚本,请使用 Shell 的参数扩展:

MYPATH="/user/dj/a/mydir"
PATH_FORMAT=${MYPATH////\/}

The substition ${parameter}is extended by the syntax ${parameter//pattern/string}which replaces allpatterns found in parameter. The patterncan be a regular expression. In your case, the patternis: /. The replace stringis: \\/.

该substition${parameter}由语法扩展${parameter//pattern/string}它取代所有中发现的模式parameter。的pattern可以是正则表达式。在你的情况下,pattern是:/。替换string为:\\/

Furthermore, avoid storing something in the variable PATH, because it is probably used by the system -- unless you want to modify the system's path variable.

此外,避免在变量中存储一些东西PATH,因为它可能被系统使用——除非你想修改系统的路径变量。

Quoting gnu.org's manual on parameter expension:

引用gnu.org的参数扩展手册:

${parameter/pattern/string}

The pattern is expanded to produce a pattern just as in filename expansion. Parameter is expanded and the longest match of pattern against its value is replaced with string. The match is performed according to the rules described below (see Pattern Matching). If pattern begins with /, all matches of pattern are replaced with string.Normally only the first match is replaced. If pattern begins with #, it must match at the beginning of the expanded value of parameter. If pattern begins with %, it must match at the end of the expanded value of parameter. If string is null, matches of pattern are deleted and the / following pattern may be omitted. If the nocasematch shell option (see the description of shopt in The Shopt Builtin) is enabled, the match is performed without regard to the case of alphabetic characters. If parameter is @or *, the substitution operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with @or *, the substitution operation is applied to each member of the array in turn, and the expansion is the resultant list.

${参数/模式/字符串}

模式被扩展以产生一个模式,就像在文件名扩展中一样。参数被扩展,模式与其值的最长匹配被替换为字符串。匹配是根据下面描述的规则执行的(参见模式匹配)。如果模式以 开头/,则模式的所有匹配项都将替换为字符串。通常只替换第一个匹配项。如果模式以 开头#,则它必须匹配参数扩展值的开头。如果模式以%, 它必须匹配在参数扩展值的末尾。如果 string 为空,则删除模式的匹配项,并且可以省略 / 后面的模式。如果启用 nocasematch shell 选项(请参阅 Shopt Builtin 中的 shopt 描述),则执行匹配时不考虑字母字符的大小写。如果参数是@*,则对每个位置参数依次进行替换操作,展开的结果就是列表。如果参数是一个以@or*为下标的数组变量,则对数组中的每个成员依次进行替换操作,展开后就是结果列表。

回答by looper

sed "s%/%\\\/%g"

. As Simple as that

. 就如此容易