bash replace string code example
Example 1: bash replace substring
echo [string] | sed "s/[original]/[target]/g"
Example 2: bash script: replace . with :
firstString="I love Suzi and Marry"
secondString="Sara"
echo "${firstString/Suzi/$secondString}"
message='The secret code is 12345'
echo "${message//[0-9]/X}"
Example 3: bash replace substring in string
#!/bin/bash
firstString="I love Suzi and Marry"
secondString="Sara"
echo "${firstString/Suzi/$secondString}"
Example 4: bash replace beginning of string
$ cat shortest.sh
filename="bash.string.txt"
echo ${filename#*.}
echo ${filename%.*}
$ ./shortest.sh
After deletion of shortest match from front: string.txt
After deletion of shortest match from back: bash.string
Example 5: bash search and replace text in file
awk '{gsub(regex, substitution_text, $field#); print $0;}' input_file
awk '{gsub(" ","",$0); print $0;}' input_file