【问题标题】:sed command is not working on windows 10sed 命令在 Windows 10 上不起作用
【发布时间】:2017-01-23 09:22:53
【问题描述】:

我尝试在我的 Windows 10 机器上运行以下命令。在这里,新的文本文件包含一些类似“你好吗?”的文本。我想在同一个文件中替换字符串 'how'->'where' 而不创建新文件。但它显示错误。有什么可以解决的吗?

sed -i s/how/where/ new.txt

sed: 无效选项 -- i

【问题讨论】:

  • sed --version 的输出是什么?
  • @spasic GNU sed 3.02 版
  • 我认为该版本没有就地编辑选项。您可以尝试解决方法:sed 's/how/where/' new.txt > tmp.txt && mv tmp.txt new.txt 或使用perl(如果可用)
  • 从哪里获得最新的 sed?@spasic
  • 我从 unxutils.sourceforge.net 下载了 UnxUpdates.zip,它包含 GNU sed version 4.0.7,它支持 -i 选项。你能确定你安装的是最新版本吗?

标签: bash shell sed


【解决方案1】:

您的 sed (GNU sed version 3.02) 版本不支持 -i 选项。您可以更新到更新版本的 sed (version 4.2.1 is available here),或者通过重定向到临时文件然后将其复制到源文件来解决此问题:

C:\>cat.exe foo.txt
foo
how
bar
baz
foo how bar

C:\>sed.exe s/how/where/ foo.txt > foo2.txt

C:\>move /Y foo2.txt foo.txt
        1 file(s) moved.

C:\>cat.exe foo.txt
foo
where
bar
baz
foo where bar

【讨论】:

    猜你喜欢
    • 2020-05-19
    • 2020-09-27
    • 1970-01-01
    • 2021-07-20
    • 2022-08-18
    • 2017-09-01
    • 2016-04-16
    • 1970-01-01
    • 2014-09-16
    相关资源
    最近更新 更多