1

For some mysterious reason, some elements in my CSV data appear as s/stWgvN52??f2& ?" instead of stWgvN522tw0JtZZnyXj, which messes up the file because I have ; set as the CSV delimiter.

I attempted to replace the defective string using sed as follows:

$ sed -i 's/stWgvN52??f2& ?"/stWgvN522tw0JtZZnyXj/g' file.csv

but I get the following error: sed: 1: "access_logs_2014-04.csv": command a expects \ followed by text

What is the reason?

biohazard
  • 2,017
  • 10
  • 28
  • 41
  • 2
    possible duplicate of [sed command failing on Mac, but works on Linux](http://stackoverflow.com/questions/4247068/sed-command-failing-on-mac-but-works-on-linux) – Barmar Jun 07 '14 at 15:12

1 Answers1

6

When you use the -i option, you have to specify the extension of the backup file that gets made. Some versions of sed expect the extension directly appended to the -i option, so what you wrote would work. But other versions (like the version on OS X) require it to be a separate option, so you have to write:

sed -i '' 's/stWgvN52??f2& ?"/stWgvN522tw0JtZZnyXj/g' file.csv

to specify that you don't want a backup file.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • If you escape `?` in a normal regexp it behaves as a extended regexp and becomes a metacharacter that accepts zero or one of the preceeding character (GNU sed). So unless you are using the extended regexp switch `-r` (perhaps -E in other seds) you do not need to escape it. – potong Jun 08 '14 at 07:26
  • It's so hard to remember the details of every regexp variation. I thought `?` was a basic RE, not extended RE. – Barmar Jun 08 '14 at 23:00