2

I'm using Window 7.

sed -V

gives me

GNU sed version 3.02

I have a text file with:

foo bar

In command line if I write:

sed s/foo/tofu/ test.txt

I get printed on stdout;

tofu bar

However, I want to edit this file inplace, so using the -i option:

sed -i s/foo/tofu/ test.txt

gives me sed: invalid option -- i

What am I missing?

dwjohnston
  • 7,389
  • 20
  • 81
  • 147
  • Since it's probably a rare Windows 7 user who uses sed, I'm not sure how much help you'll get. It works fine on linux. – ooga Jul 03 '14 at 03:43
  • @ooga Thanks. For now this solution works for me: http://stackoverflow.com/a/20999154/1068446 – dwjohnston Jul 03 '14 at 03:50
  • Perhaps you should download a later version. I'm using GNU sed 4.2.1 from 2009. It objects to `sed -V` and wants `sed --version`. Your `sed -i` line worked using that version for me. – Magoo Jul 03 '14 at 04:55

2 Answers2

1

While I can't guarantee this will work because I'm not running it on Windows 7 but have you considered using redirection and then swapping? Meaning something like this:
sed s/foo/tofu/ test.txt > tmp.txt && mv tmp.txt test.txt
While this is a very dirty way of doing things I think it might work. You can also try doing
sed s/foo/tofu/ test.txt > test.txt
but I feel that might lead to some errors because you're reading and writing on the same file.

skamazin
  • 746
  • 5
  • 12
  • 1
    `sed s/// test > test` cannot work: the shell will process the redirection **first** and the file will get truncated to 0 bytes. Then, when sed starts, its reading from an **empty file**. – glenn jackman Jul 24 '14 at 14:30
0

It's the sed version. Try the one found here: http://unxutils.sourceforge.net/UnxUpdates.zip