Skip to main content

I used this:

grep -r "old_string" -l | tr '\n' ' ' | xargs sed -i 's/old_string/new_string/g'
  1. List all files that contain old_string.

  2. Replace newline in result with spaces (so that the list of files can be fed to sed.

  3. Run sed on those files to replace old string with new.

Update: The above result will fail on filenames that contain whitespaces. Instead, use:

grep --null -lr "old_string" | xargs --null sed -i 's/old_string/new_string/g'

grep --null -lr "old_string" | xargs --null sed -i 's/old_string/new_string/g'

I used this:

grep -r "old_string" -l | tr '\n' ' ' | xargs sed -i 's/old_string/new_string/g'
  1. List all files that contain old_string.

  2. Replace newline in result with spaces (so that the list of files can be fed to sed.

  3. Run sed on those files to replace old string with new.

Update: The above result will fail on filenames that contain whitespaces. Instead, use:

grep --null -lr "old_string" | xargs --null sed -i 's/old_string/new_string/g'

I used this:

grep -r "old_string" -l | tr '\n' ' ' | xargs sed -i 's/old_string/new_string/g'
  1. List all files that contain old_string.

  2. Replace newline in result with spaces (so that the list of files can be fed to sed.

  3. Run sed on those files to replace old string with new.

Update: The above result will fail on filenames that contain whitespaces. Instead, use:

grep --null -lr "old_string" | xargs --null sed -i 's/old_string/new_string/g'

added 189 characters in body
Source Link
o_o_o--
  • 815
  • 1
  • 8
  • 9

I used this:

grep -r "old_string" -l | tr '\n' ' ' | xargs sed -i 's/old_string/new_string/g'
  1. List all files that contain old_string.

  2. Replace newline in result with spaces (so that the list of files can be fed to sed.

  3. Run sed on those files to replace old string with new.

Update: The above result will fail on filenames that contain whitespaces. Instead, use:

grep --null -lr "old_string" | xargs --null sed -i 's/old_string/new_string/g'

I used this:

grep -r "old_string" -l | tr '\n' ' ' | xargs sed -i 's/old_string/new_string/g'
  1. List all files that contain old_string.

  2. Replace newline in result with spaces (so that the list of files can be fed to sed.

  3. Run sed on those files to replace old string with new.

I used this:

grep -r "old_string" -l | tr '\n' ' ' | xargs sed -i 's/old_string/new_string/g'
  1. List all files that contain old_string.

  2. Replace newline in result with spaces (so that the list of files can be fed to sed.

  3. Run sed on those files to replace old string with new.

Update: The above result will fail on filenames that contain whitespaces. Instead, use:

grep --null -lr "old_string" | xargs --null sed -i 's/old_string/new_string/g'
Source Link
o_o_o--
  • 815
  • 1
  • 8
  • 9

I used this:

grep -r "old_string" -l | tr '\n' ' ' | xargs sed -i 's/old_string/new_string/g'
  1. List all files that contain old_string.

  2. Replace newline in result with spaces (so that the list of files can be fed to sed.

  3. Run sed on those files to replace old string with new.