9

I am trying to add a file in place with sed and I keep getting this error message

sed: 1: "test.txt": undefined label 'est.txt'

This is the command I am using

Desktop > sed -i 's/White/Black/' test.txt

I am not sure what I am doing incorrect here.... Any help will be really appreciated! Thanks in advance!

Eldan Shkolnikov
  • 441
  • 1
  • 6
  • 13

1 Answers1

16

Some versions of sed require an argument for -i. Try:

sed -i.bak  's/White/Black/' test.txt

Or (OSX/BSD only),

sed -i ''  's/White/Black/' test.txt

As you know, -i tells sed to edit in place. The argument the extension to use for the back-up of the original file. The argument is the empty string, your sed will likely not keep a back-up file.

GNU sed does not require an argument for -i. BSD sed (Mac OSX) does.

Details

Consider the command:

sed -i 's/White/Black/' test.txt

If your sed requires an argument to -i, then it would believe that s/White/Black/ was that argument. Consequently, it believes that the next argument, test.txt, is the sed command that it should execute. This would be interpreted as the "test" command t with the intention of jumping to the label est.txt if the test is true. No such label has been defined. Hence the error message:

sed: 1: "test.txt": undefined label 'est.txt'

Alternative

ThatOtherGuy reports that the following will run without error on all platforms:

sed -i -e 's/White/Black/' test.txt

ThatOtherGuy notes that the behavior of this line is, however, platform-dependent. With BSD sed, it creates a back-up file with a -e extension. With GNU sed, no back-up is created.

John1024
  • 109,961
  • 14
  • 137
  • 171
  • Aha! That makes a lot sense. Thank you so much for the explanation dude. And btw both commands you specified worked well! – Eldan Shkolnikov Nov 14 '14 at 22:34
  • 1
    `sed -i -e '...'` can be used on both GNU and OSX. – that other guy Nov 14 '14 at 23:25
  • @thatotherguy So, on OSX, it is not possible to use `-e` for a back-up extension? Very good. I updated the answer to include your code. – John1024 Nov 14 '14 at 23:33
  • @John1024 It is! The thing is that on OS X it uses `-e` as a backup suffix while on GNU it sees `-e` as introducing the sed command, and either way it's valid and modifies the file. – that other guy Nov 14 '14 at 23:46