【发布时间】:2017-02-15 19:43:28
【问题描述】:
我正在尝试替换 Windows 路径中的反斜杠,以便可以将路径粘贴到 Filezilla 中以打开文件夹,而无需浏览目录结构。我使用以下命令:
echo '\path\to\the\05_directory' | sed -e 's/\\/\//g'
我的预期结果是
/path/to/the/05_directory
但我得到了
/path o he_directory
似乎\t 和\05 被解释为文字字符串以外的东西。
为什么会这样?我该如何解决这个问题?
【问题讨论】:
-
你在用什么?在linux命令行
echo '\path\to\the\05_directory' | sed -e 's/\\/\//g'输出/path/to/the/05_directory -
您确定是
echo而不是echo -e? -
顺便说一句,对于单个字符,您可以使用 tr:
tr '\\' '/',而不是使用 sed -
对不起,我忘了补充:我在Windows下使用的是babun(一个类似于cygwin的命令行模拟器)。使用
tr '\\' '/'会得到相同的结果。 -
@msoutopico,您可以尝试使用文件输入而不是 echo 的 sed 命令吗?例如:
sed -e 's/\\/\//g' file.txt其中 file.txt 包含\path\to\the\05_directory
标签: regex sed command-line backslash babun