bash:strings:find_replace
BASH - Strings - Find & Replace
var="abcdabe" echo ${var/ab/fg}
returns:
fgcdabe
NOTE: Only the first match is changed.
To change all occurrences use:
var="abcdabe" echo ${var//ab/fg}
returns:
fgcdfge
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/strings/find_replace.txt · Last modified: 2021/02/10 16:04 by peter