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

Card Puncher Data Processing


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 -i@ sed -i 's/old-text/new-text/g' @

Discover More
Card Puncher Data Processing
Shell Data Processing - Stream

This page is the creation of Stream in the Shell context. You start a stream with an initialization function that creates a standard output such as: grep ... You connect then: the first...

Share this page:
Follow us:
Task Runner