Vijay Vishnu Vijay Vishnu - 2 months ago 28
Bash Question

sed command is not working on windows 10

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?

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


sed: invalid option -- i

enter image description here

Tim Tim
Answer

Your version of sed (GNU sed version 3.02), does not support the -i option. 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:

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