bash:files:find_replace_a_string_within_a_file

This is an old revision of the document!


BASH - Files - Find & Replace a String within a File

Changing contents within a file

contents=$(< infile.txt)
$ echo "${contents/$old/$new}"

NOTE: This reads the file into a Bash variable and uses parameter expansion.

  • infile.txt: The input file here is named infile.txt.
  • $old: The string to replace.
  • $new: The replacement string.

To change the file in-place:

echo "${contents/$old/$new}" > infile.tmp && mv infile.tmp infile.txt
bash/files/find_replace_a_string_within_a_file.1612973209.txt.gz · Last modified: 2021/02/10 16:06 by peter

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki