sed command is not working on windows 10

10,260

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
Share:
10,260
VijayVishnu
Author by

VijayVishnu

good learner

Updated on June 04, 2022

Comments

  • VijayVishnu
    VijayVishnu almost 2 years

    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

    • Sundeep
      Sundeep over 7 years
      what is the output of sed --version ?
    • VijayVishnu
      VijayVishnu over 7 years
      @spasic GNU sed version 3.02
    • Sundeep
      Sundeep over 7 years
      I think that version doesn't have inplace editing option.. you can try a workaround: sed 's/how/where/' new.txt > tmp.txt && mv tmp.txt new.txt or use perl if it is available
    • VijayVishnu
      VijayVishnu over 7 years
      where to get the latest sed?@spasic
    • VijayVishnu
      VijayVishnu over 7 years
      unxutils.sourceforge.net this was having latest version 3.02 only
    • Sundeep
      Sundeep over 7 years
    • Tim
      Tim over 7 years
      I downloaded UnxUpdates.zip from unxutils.sourceforge.net and it contained GNU sed version 4.0.7, which supports the -i option. Can you make sure you have the latest version installed?