bash sed 命令在 Windows 10 上不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39509601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 15:10:54  来源:igfitidea点击:

sed command is not working on windows 10

bashshellsed

提问by VijayVishnu

I tried to run the below command in my windows 10 machine. Here, new text file contains some text like this 'how are you?'. I want to replace the string 'how'->'where' in that same file without creating a new file. But It shows error. Any comments to resolve it?

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

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

sed: invalid option -- i

sed: 无效选项——我

enter image description here

在此处输入图片说明

采纳答案by Tim

Your version of sed (GNU sed version 3.02), does not support the -ioption. You can either update to a more recent version of sed (version 4.2.1 is available here), or work around the issue by redirecting to a temporary file and then copying it to the source file:

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

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