0

I have a lot of \ in a file that I would like to remove using sed. Here is my command:

sed -i.bak 's/\\\//g' myFile

And I get the following error:

sed: 1: "i.bak": command i expects \ followed by text.

Shouldn't it work. I tried:

sed -i.bak 's/\\//g' myFile

But I get the same error.

Thank you.

Johnathan
  • 1,877
  • 4
  • 23
  • 29
  • 5
    This is because osx does not admit `-i.bak`. You need to indicate `-i 'bak'`. There are many questions like this, cannot find the proper one now. – fedorqui Oct 07 '14 at 15:10
  • possible duplicate of [Getting an error with sed expression](http://stackoverflow.com/questions/17470697/getting-an-error-with-sed-expression) – fedorqui Oct 07 '14 at 16:10
  • It could be a shell issue, did you try sed -i'.bak' ? –  Nov 09 '14 at 21:56

1 Answers1

1

When you are using sed on OS X and when you want to change file in-place using -i flag, then you need to specify the extension for saving backups of that file.

Check out man sed section about -i flag:

 -i extension
         Edit files in-place, saving backups with the specified extension.  If a zero-length extension is
         given, no backup will be saved.  It is not recommended to give a zero-length extension when in-
         place editing files, as you risk corruption or partial content in situations where disk space is
         exhausted, etc.

So, the correct command would be:

sed -i "bak" "s/\\\//g" myFile

And if you do not want to make backup file, you could just write like this:

sed -i "" "s/\\\//g" myFile

If you want to make your script platform independent you can check the $OSTYPE environment variable for that:

if [[ "$OSTYPE" == "darwin"* ]]; then
  sed -i "" "s/\\\//g" myFile
else
  sed -i "s/\\\//g" myFile
fi