【问题标题】:Using sed command how can I replace Linux path to Windows path使用 sed 命令如何将 Linux 路径替换为 Windows 路径
【发布时间】:2017-07-08 11:33:35
【问题描述】:

我想将 Linux 路径更改为 Windows 路径,使用“sed”命令,例如:

Linux 路径:/opt/test/dash/apps/tomcat 到 Windows 路径: c:\\test\\dash\\apps\\tomcat

我试过了:

sed -i 's|'/opt/test/dash/apps/tomcat'|'c:\\\\\\\test\\\\\\\dash\\\\\\\apps\\\\\\\tomcat'|g' /filename - But no luck!!

我真正想要的所有 /opt/ 都应该替换为 c:\\ 并且其余的“/”应该替换为“\\”。

注意:我正在使用 ssh2_exec 远程执行此命令,除上述之外,所有“sed”命令都可以正常工作。

提前致谢!!

【问题讨论】:

标签: linux bash awk sed


【解决方案1】:

我会分两步完成:

$>echo '/opt/test/dash/apps/tomcat' | sed 's#/opt#c:#g'|sed 's#/#\\\\#g'
c:\\test\\dash\\apps\\tomcat

先把/opt改成c:,再把/改成\,这样你就得转义了

【讨论】:

  • 您好,谢谢,但是如何将相同的命令应用于一组文件?比如:sed 's#/opt#c:#g'|sed 's#/#\\\\#g' *.properties
  • 我会使用 for 循环:for i in *properties; do sed -i 's#/opt#c:#g'|sed 's#/#\\\\#g' $i;done
【解决方案2】:

我会使用正则表达式等等:

sed -r 's@/(.*)/(.*)/(.*)/(.*)/(.*)@C:\\\\\2\\\\\3\\\\\4\\\\\5@'

使用 -r 启用正则表达式的解释和 @ 作为 sed 分隔符,将路径分成 5 部分,然后在翻译部分中使用 \1 \2 等引用它们。

【讨论】:

  • 谢谢,但是如何执行相同的命令来更改文件中的内容,例如: sed -r 's@/(.*)/(.*)/(.*)/ (.*)/(.*)@C:\\\\\2\\\\\\3\\\\\4\\\\\5@'
  • 使用以下命令 sed -i -r 's@/(.*)/(.*)/(.*)/(.*)/(.*)@C:\ \\\\2\\\\\3\\\\\4\\\\\5@' *.properties
猜你喜欢
  • 2017-12-17
  • 1970-01-01
  • 1970-01-01
  • 2012-01-11
  • 2011-01-24
  • 1970-01-01
  • 1970-01-01
  • 2012-08-17
  • 2013-11-28
相关资源
最近更新 更多