替换路径中的反斜杠的问题
Issues replacing backslashes in paths
我正在尝试替换 Windows 路径中的反斜杠,以便我可以将路径粘贴到 Filezilla 中以打开文件夹,而无需浏览目录结构。我使用以下命令:
echo '\path\to\the_directory' | sed -e 's/\/\//g'
我的预期结果是
/path/to/the/05_directory
但我得到
/path o he_directory
似乎 \t
和
被解释为不同于文字字符串的东西。
为什么会这样?我该如何解决这个问题?
您可以使用 printf "%q"
打印文字 \
而不是将它们解释为制表符:
printf "%q" '\path\to\the_directory'
\path\to\the\05_directory
然后你可以使用sed
得到你的输出:
printf "%q" '\path\to\the_directory' | sed -e 's|\\|/|g'
/path/to/the/05_directory
"%q"
字段准备了一个要在 shell 中使用的字符串。这当然意味着 ' '
将被转义:
printf "%q" '\path\to\the directory'
\path\to\the\05\ directory
您可以单独清理的:
printf "%q" '\path\to\the directory' | sed -e 's|\\|/|g; s|\||g'
/path/to/the/05 directory
我正在尝试替换 Windows 路径中的反斜杠,以便我可以将路径粘贴到 Filezilla 中以打开文件夹,而无需浏览目录结构。我使用以下命令:
echo '\path\to\the_directory' | sed -e 's/\/\//g'
我的预期结果是
/path/to/the/05_directory
但我得到
/path o he_directory
似乎 \t
和 被解释为不同于文字字符串的东西。
为什么会这样?我该如何解决这个问题?
您可以使用 printf "%q"
打印文字 \
而不是将它们解释为制表符:
printf "%q" '\path\to\the_directory'
\path\to\the\05_directory
然后你可以使用sed
得到你的输出:
printf "%q" '\path\to\the_directory' | sed -e 's|\\|/|g'
/path/to/the/05_directory
"%q"
字段准备了一个要在 shell 中使用的字符串。这当然意味着 ' '
将被转义:
printf "%q" '\path\to\the directory'
\path\to\the\05\ directory
您可以单独清理的:
printf "%q" '\path\to\the directory' | sed -e 's|\\|/|g; s|\||g'
/path/to/the/05 directory