How to replace in bulk a text in multiple file with a bash pipeline

About

This article is an example of a shell pipeline for data processing.

It will show you how to replace in bulk a text in multiple file.

Creation of the pipeline

Select the file that contains the text to replace

With grep, you can list the path of files containing a pattern

grep -rli 'old-text' *

where the options:

  • i will ignore the case
  • r will recursively search from the actual directory
  • l will suppress the normal output and instead print the name of each file

Replace the text in a file

With sed, you can replace an old text to a new one

sed -i 's/old-text/new-text/g' /file/path
# i: in place, the file will be overwritten

Put it together and create the pipeline

  • Because sed does not take as input a list of files, you need xargs to put it in the stream
grep -rli 'old-text' * | xargs [email protected] sed -i 's/old-text/new-text/g' @

Powered by ComboStrap