2

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

VijayVishnu
  • 537
  • 2
  • 11
  • 34
  • what is the output of `sed --version` ? – Sundeep Sep 15 '16 at 11:12
  • @spasic GNU sed version 3.02 – VijayVishnu Sep 15 '16 at 11:58
  • 1
    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 – Sundeep Sep 15 '16 at 12:01
  • where to get the latest sed?@spasic – VijayVishnu Sep 15 '16 at 12:59
  • http://unxutils.sourceforge.net/ this was having latest version 3.02 only – VijayVishnu Sep 15 '16 at 13:00
  • no idea, this might help: http://unix.stackexchange.com/questions/111673/aixs-sed-in-place-editing and https://stackoverflow.com/questions/7232797/sed-on-aix-does-not-recognize-i-flag – Sundeep Sep 15 '16 at 13:07
  • 1
    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? – Tim Sep 16 '16 at 00:08

1 Answers1

1

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
Tim
  • 4,790
  • 4
  • 33
  • 41