1

I want to replace the word willow with oak throughout my code. This change needs to be made to multiple files at different levels.

I have attempted to use the answers from: How to do a recursive find/replace of a string with awk or sed?

Especially, grep -rl willow . | xargs sed -i -e '' 's/willow/oak/g'


As I am using a Mac (from: sed -i command for in-place editing to work with both GNU sed and BSD/OSX), I have used the extra -e '' commands. However, I am getting

s/willow/oak/g: No such file or directory


I have also tried:

grep -rl willow . | xargs sed -i '' 's/willow/oak/g'

which (due to the OS) gave: sed: RE error: illegal byte sequence.


How can I recursively go through all the files in different dictionaries on a Mac to replace all instances of willow?

Wychh
  • 656
  • 6
  • 20

1 Answers1

1
grep -rl willow . | xargs sed -i -e '' 's/willow/oak/g'

I have used the extra -e '' commands.

You have misread or misinterpreted the answers from which you got that idea, creating an additional problem instead of solving one. With that command, not only does MacOS sed interpret the -e as the extension for backup files and 's/willow/oak/g' as an input file name, but it also interprets '' as a (zero-length) file name. The latter is what your error message is about.

I have also tried:

grep -rl willow . | xargs sed -i '' 's/willow/oak/g'

Per its manual page, if you use the -i or -f option with Mac sed then you must use -e if you want to specify a command via the command line. That is, you want this:

grep -rl willow . | xargs sed -i '' -e 's/willow/oak/g'

That specifies an empty backup extension for -i, thus suppressing the generation of backup files, and specifies a sed command on the command line via -e.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157