Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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 .
share|improve this question
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 Jan 7 '13 at 3:16
    
Mac. where do I put this -i.bak???? –  Digital Robot Jan 7 '13 at 3:21
    
Replace the plain -i with it. find [...] | xargs -0 sed -i.bak 's/AAA/BBB/g' –  Kevin Jan 7 '13 at 3:24
    
ok, now I get another error: sed: RE error: illegal byte sequence –  Digital Robot Jan 7 '13 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 Jan 7 '13 at 3:39
show 1 more comment

2 Answers

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'
share|improve this answer
add comment

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'
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.