47
grep -l \'texttofind\' * | xargs sed -i 's/toreplace/replacewith/g'

Im getting this error when I run the above command in the terminal.

sed: 1: "forkliftDailyChecklistW ...": invalid command code f

I've looked at several forums and have found nothing about code f. Any help/insight would be appreciated

DavidPostill
  • 7,734
  • 9
  • 41
  • 60
Jonny Forney
  • 1,988
  • 3
  • 17
  • 15
  • 2
    Please show the exact sed command you're using (instead of `toreplace` and `replacewith`). Wildly guessing: is there a `/` in one of the terms? – Wintermute Mar 16 '15 at 16:24
  • Yes. Assuming the output of `grep`is correct you should definitely check for `/` in the arguments of `sed`. If this is the case, try using the syntax `'s_toreplace_replacewith_g'`. – TomCho Mar 16 '15 at 16:29
  • Here's the command: grep -l \'/usr/local/www/apache22/data/htconfig/dbconfigure.php\' * | xargs sed -i 's/\/usr\/local\/www\/apache22\/data/'\''\.\$_SERVER\['\''DOCUMENT_ROOT'\''\]\.'\''/g' – Jonny Forney Mar 16 '15 at 17:10

1 Answers1

90

I figured out what was wrong. I needed to add '' after the -i and before the 's/../../':

grep -l \'texttofind\' * | xargs sed -i '' 's/toreplace/replacewith/g'
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Jonny Forney
  • 1,988
  • 3
  • 17
  • 15
  • 1
    I knew lunch would fix it! – Tdorno Mar 16 '15 at 17:26
  • 58
    You are probably running on a Mac, or a BSD system. The BSD `sed` command takes the `-i` option but requires a suffix for the backup (but an empty suffix is permitted). Providing basic platform information often helps. – Jonathan Leffler Mar 16 '15 at 17:44