2

I have this line

find . -type f \( ! -name '*.plist' \) -and \( ! -name '*.mp4' \) -and \( ! -name '.DS*' \) -print0 | xargs -0 sed -i 's/AAA/BBB/g'

my intention is from the current directory recursively change all occurrences of AAA to BBB in all files except those with the following extensions: .plist, .mp4 and .DS*

but I get the message

sed: 1: "./myDir/pro ...": invalid command code .
6
  • 1
    Are you on a mac or other BSD? I've noticed you have to include a suffix with -i on those. Try -i.bak
    – Kevin
    Commented Jan 7, 2013 at 3:16
  • Mac. where do I put this -i.bak????
    – Duck
    Commented Jan 7, 2013 at 3:21
  • Replace the plain -i with it. find [...] | xargs -0 sed -i.bak 's/AAA/BBB/g'
    – Kevin
    Commented Jan 7, 2013 at 3:24
  • ok, now I get another error: sed: RE error: illegal byte sequence
    – Duck
    Commented Jan 7, 2013 at 3:25
  • Looks like someone on SO had that problem, answer here. Add LANG=C before the xargs: find [...] | LANG=C xargs -0 [...]
    – Kevin
    Commented Jan 7, 2013 at 3:39

2 Answers 2

1

The easiest solution is to use this:

find . -type f \( ! -name '*.plist' \) -and \( ! -name '*.mp4' \) -and \( ! -name '.DS*' \) -print0 | xargs -0 perl -pi -w -e  's/AAA/BBB/g'
1

Unlike GNU sed, the sed that ships with Mac OSX requires an argument to the -i option, specifying the backup suffix; to suppress the backup you have to pass an empty string (''). Therefore, sed was interpreting your s/AAA/BBB/ as the backup suffix and taking your fist file name (./myDir/pro ...) as the command to execute. Of course, . is not a valid sed command, causing the error. Keeping that in mind, the following will work:

find [...] | xargs -0 sed -i.bak 's/AAA/BBB/g'

Or, if you insist on suppressing the backup

find [...] | xargs -0 sed -i '' 's/AAA/BBB/g'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .