replace text in a file

Change every place a word or a phrase appears in a file, keeping a copy if you like.

  • text
  • files

Steps

  1. sed 's/old/new/g' file.txt

    show the file with every old turned into new, without changing it

  2. sed -i 's/old/new/g' file.txt

    change the file itself

  3. sed -i.bak 's/old/new/g' file.txt

    the same, keeping the old one as file.txt.bak

  4. sed -i '0,/old/s//new/' file.txt

    only the first one in the file (GNU sed)

  5. sed -i 's|/old/path|/new/path|g' file.txt

    text with slashes: another separator

Notes

  • macOS: sed -i '' 's/old/new/g' file.txt.
  • sed 's/…/…/' file > file empties the file: write to a new one, or use -i.

Commands used

  • sed
  • perl

Each has a page in lore: lore sed opens it.