简体   繁体   English

替换 Bash 中的斜杠

[英]Replace slash in Bash

Let's suppose I have this variable:假设我有这个变量:

DATE="04\Jun\2014:15:54:26"

Therein I need to replace \\ with \\/ in order to get the string:其中我需要用\\/替换\\以获得字符串:

"04\/Jun\/2014:15:54:26"

I tried tr as follows:我试过tr如下:

echo "04\Jun\2014:15:54:26" | tr  '\' '\\/'

But this results in: "04\\Jun\\2014:15:54:26" .但这会导致: "04\\Jun\\2014:15:54:26"

It does not satisfy me.它不满足我。 Can anyone help?任何人都可以帮忙吗?

No need to use an echo + a pipe + sed.无需使用回声 + 管道 + sed。

A simple substitution variable is enough and faster:一个简单的替换变量就足够了,而且速度更快:

echo ${DATE//\//\\/}

#> 04\/Jun\/2014:15:54:26

Use sed for substitutions:使用sed进行替换:

sed 's#/#\\/#g' < filename.txt > newfilename.txt

You usually use " / " instead of the " # ", but as long as it is there, it doesn't matter.你通常使用“ / ”而不是“ # ”,但只要它在那里,就没有关系。

I am writing this on a windows PC so I hope it is right, you may have to escape the slashes with another slash.我在 Windows PC 上写这个,所以我希望它是正确的,你可能不得不用另一个斜线来逃避斜线。

sed explained, the -e lets you edit the file in place. sed解释说, -e允许您就地编辑文件。 You can use -i to create a backup automatically.您可以使用-i自动创建备份。

sed -e s/STRING_TO_REPLACE/STRING_TO_REPLACE_IT/g index.html

here you go:干得好:

kent$  echo "04/Jun/2014:15:54:26"|sed 's#/#\\/#g'  
04\/Jun\/2014:15:54:26

your tr line was not correct, you may mis-understand what tr does, tr 'abc' 'xyz' will change a->x, b->y, c->z ,not changing whole abc->xyz ..你的tr行不正确,你可能误解了tr作用, tr 'abc' 'xyz'会改变a->x, b->y, c->z ,而不是改变整个abc->xyz ..

This has not been said in other answers so I thought I'd add some clarifications:其他答案中没有提到这一点,所以我想我会添加一些说明:

tr uses two sets of characters for replacement, and the characters from the first set are replaced with those from the second set in a one-to-one correspondance. tr使用两组字符进行替换,第一组中的字符以一一对应的方式替换为第二组中的字符。 The manpage states that联机帮助页指出

SET2 is extended to length of SET1 by repeating its last character as necessary.通过根据需要重复其最后一个字符,将 SET2 扩展到 SET1 的长度。 Excess characters of SET2 are ignored. SET2 的多余字符将被忽略。

Example:例子:

echo abca | tr ab de    # produces decd
echo abca | tr a de     # produces dbcd, 'e' is ignored
echo abca | tr ab d     # produces ddcd, 'd' is interpreted as a replacement for 'b' too

When using sed for substitutions, you can use another character than '/' for the delimiter, which will make your expression clearer (I like to use ':', @n34_panda proposed '#' in their answer).使用sed进行替换时,您可以使用 '/' 以外的其他字符作为分隔符,这将使您的表达更清晰(我喜欢使用 ':',@n34_panda 在他们的回答中建议使用 '#')。 Don't forget to use the /g modifier to replace all occurences: sed 's:/:\\\\/:g' with quotes or sed s:/:\\\\\\\\/:g without (backslashes have to be escaped twice).不要忘记使用/g修饰符替换所有出现的: sed 's:/:\\\\/:g'带引号或sed s:/:\\\\\\\\/:g不带(反斜杠必须转义两次)。

Finally your shortest solution will probably be @Luc-Olivier's answer, involving substitution, in the following form (don't forget to escape forward slashes too when part of the expected pattern):最后,您最短的解决方案可能是@Luc-Olivier 的答案,涉及替换,形式如下(在预期模式的一部分时也不要忘记转义正斜杠):

echo ${variable/expected/replacement}     # will replace one occurrence
echo ${variable//expected/replacement}    # will replace all occurrences

您还可以转义斜杠,其可读性略低于散列:

echo "04/Jun/2014:15:54:26" | sed 's/\//\\\//g'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM