bash 使用 sed 用正斜杠替换所有反斜杠
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6852951/
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
Use sed to replace all backslashes with forward slashes
提问by Emily
I want to be able to use sed to take an input such as:
我希望能够使用 sed 进行输入,例如:
C:\Windows\Folder\File.txt
to
到
C:/Windows/Folder/File.txt
回答by glenn Hymanman
for just translating one char into another throughout a string, tr
is the best tool:
只需将一个字符转换为整个字符串中的另一个字符,这tr
是最好的工具:
tr '\' '/'
回答by Emiliano Poggi
sed
can perform text transformations on input stream from a file or from a pipeline. Example:
sed
可以对来自文件或管道的输入流执行文本转换。例子:
echo 'C:\foo\bar.xml' | sed 's/\/\//g'
gets
得到
C:/foo/bar.xml
回答by user541686
Just use:
只需使用:
sed 's.\./.g'
There's no reason to use /
as the separator in sed
. But if you really wanted to:
没有理由/
在sed
. 但如果你真的想:
sed 's/\/\//g'
回答by Jimmy
$ echo "C:\Windows\Folder\File.txt" | sed -e 's/\/\//g'
C:/Windows/Folder/File.txt
The sed command in this case is 's/OLD_TEXT/NEW_TEXT/g'
.
这种情况下的 sed 命令是's/OLD_TEXT/NEW_TEXT/g'
.
The leading 's' just tells it to search for OLD_TEXT and replace it with NEW_TEXT.
领先的 's' 只是告诉它搜索 OLD_TEXT 并将其替换为 NEW_TEXT。
The trailing 'g' just says to replace all occurrences on a given line, not just the first.
尾随的 'g' 只是表示替换给定行上的所有事件,而不仅仅是第一行。
And of course you need to separate the 's', the 'g', the old, and the new from each other. This is where you must use forward slashes as separators.
当然,您需要将“s”、“g”、旧的和新的彼此分开。这是您必须使用正斜杠作为分隔符的地方。
For your case OLD_TEXT == '\'
and NEW_TEXT == '/'
. But you can't just go around typing slashes and expecting things to work as expectedbe taken literally while using them as separators at the same time. In general slashes are quite special and must be handled as such. They must be 'escaped' (i.e. preceded) by a backslash.
对于您的情况OLD_TEXT == '\'
和NEW_TEXT == '/'
. 但是你不能只是绕过输入斜杠并期望事情按预期工作,同时将它们用作分隔符。一般来说,斜线非常特殊,必须如此处理。它们必须以反斜杠“转义”(即前面)。
So for you, OLD_TEXT == '\\'
and NEW_TEXT == '\/'
. Putting these inside the 's/OLD_TEXT/NEW_TEXT/g'
paradigm you get's/\\/\//g'
. That reads as
's
/ \\
/ \/
/ g
' and after escapes is
's
/ \
/ /
/ g
' which will replace all backslashes with forward slashes.
所以对你来说,OLD_TEXT == '\\'
和NEW_TEXT == '\/'
。把这些放在's/OLD_TEXT/NEW_TEXT/g'
你得到的范式中's/\\/\//g'
。读作
' s
/ \\
/ \/
/ g
',转义后是
' s
/ \
/ /
/ g
',它将用正斜杠替换所有反斜杠。
回答by Toby Speight
If your text is in a Bash variable, then Parameter Substitution ${var//\\//}
can replace substrings:
如果您的文本在 Bash 变量中,则参数替换${var//\\//}
可以替换子字符串:
$ p='C:\foo\bar.xml'
$ printf '%s\n' "$p"
C:\foo\bar.xml
$ printf '%s\n' "${p//\//}"
C:/foo/bar.xml
This may be leaner and clearer that filtering through a command such as tr
or sed
.
这可能比通过诸如tr
或 之类的命令进行过滤更精简、更清晰sed
。
回答by Adilson Marcelino
You can try
你可以试试
sed 's:\:\/:g'`
The first \
is to insert an input, the second \
will be the one you want to substitute.
第一个\
是插入一个输入,第二个\
是你想要替换的那个。
So it is 's ":" First Slash "\" second slash "\" ":" "\" to insert input "/" as the new slash that will be presented ":" g'
所以它是“:”第一个斜杠“\”第二个斜杠“\”“:”“\”插入输入“/”作为将呈现的新斜杠“:”g'
\ \/
And that's it. It will work.
就是这样。它会起作用。
回答by potong
This might work for you:
这可能对你有用:
sed 'y/\/\//'
回答by toddcscar
For me, this replaces one backslash with a forward slash.
对我来说,这用正斜杠替换了一个反斜杠。
sed -e "s/\\/\//" file.txt
回答by technerdius
I had to use [\\]
or [/]
to be able to make this work, FYI.
我必须使用[\\]
或[/]
能够完成这项工作,仅供参考。
awk '!/[\\]/' file > temp && mv temp file
awk '!/[\\]/' file > temp && mv temp file
and
和
awk '!/[/]/' file > temp && mv temp file
awk '!/[/]/' file > temp && mv temp file
I was using awk to remove backlashes and forward slashes from a list.
我正在使用 awk 从列表中删除反斜杠和正斜杠。