2013-03-06 110 views
0

我需要从bash中的字符串中删除转义字符。我得到的数据结构,其中包含与URL路径/逃脱,所以我接受了常规链接:如何使用sed从bash中的字符串中移除转义字符?

http://stackoverflow.com/questions/ask 

为一体,以逃脱/:

http:\/\/stackoverflow.com\/questions\/ask 

现在我需要从第二个链接删除\ 。为此我尝试使用SED

`echo '"'${paths[$index]}'"' | sed "[email protected]\\@@g"` 

但我得到一个错误:

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

如果我更换\\与IE浏览器。 _它像魅力一样工作,并删除字符串中所有出现的_。如何使用sed摆脱字符串中的转义字符?

回答

2

试试这个:

.......|sed '[email protected]\\@@g' 

或:

.......|sed "[email protected]\\\\@@g" 

编辑添加一个测试输出:

kent$ echo "http:\/\/stackoverflow.com\/questions\/ask"|sed "[email protected]\\\\@@g" 
http://stackoverflow.com/questions/ask 

kent$ echo "http:\/\/stackoverflow.com\/questions\/ask"|sed '[email protected]\\@@g' 
http://stackoverflow.com/questions/ask 
+0

这两种方式给了我同样的错误,但遵循你的逻辑,我尝试过........ | sed's @ \\\\ @@ g',它的工作原理!我明白为什么我需要\\\\。 \在字符串被视为\\由sed,所以我需要\\\\在sed查询匹配\\。虽然我不知道为什么它不适用于“”。 – user1581900 2013-03-06 10:39:31

+0

@ user1581900有趣,这两个在这里工作.. :)请参阅编辑回答 – Kent 2013-03-06 10:44:25

+0

我正在使用整个回声管道和sed之间''里面另一个回声声明使用“”。只有sed的@ \\\\ @@ g'在本声明中有效,但在终端中所有方法都有效。 – user1581900 2013-03-06 10:50:09

2

你的问题不明确圆你想要的方式所以这里是两种方式:

$ sed '[email protected]/@\\/@g' <<< "http://stackoverflow.com/questions/ask" 
http:\/\/stackoverflow.com\/questions\/ask 

$ sed '[email protected]\\/@/@g' <<< "http:\/\/stackoverflow.com\/questions\/ask" 
http://stackoverflow.com/questions/ask 
+0

他只是想删除所有反斜杠。 – Kent 2013-03-06 10:36:15

+0

@Kent:大概不是**全**! – Johnsyweb 2013-03-06 10:36:40

+0

@Johnsyweb看到他的代码是@ \\ @@ g' – Kent 2013-03-06 10:38:37

1

您不需要使用sed

paths[index]=${paths[index]//\\/} 

或简单地

echo ${paths[index]//\\/} 

看到的结果,而无需修改就地值。

0

您可以使用此:

sed '[email protected]\\@@g' 

但问题是,当你遇到你真正想要的字符串中的反斜杠,但被转义。在这种情况下:

sed 's/\\\\/\x1/' |sed 's/[\]//g' | sed 's/\x1/\\/g' 

替换双反斜杠与一个临时字符[SOH],取代所有其他反斜杠,然后还原所需要反斜线。

相关问题