replace string in shell script code example
Example 1: how to find and replace a string in a file using shell script
# replaces ALL(g i.e. global) ouccurences of "original_string" with "new_string",
# in the file file_name.ext_name
sed -i 's/original_string/new_string/g' file_name.ext_name
Example 2: bash script: replace . with :
#To replace the first occurrence of a pattern with a given string,
#use ${parameter/pattern/string}:
#!/bin/bash
firstString="I love Suzi and Marry"
secondString="Sara"
echo "${firstString/Suzi/$secondString}"
# prints 'I love Sara and Marry'
#To replace all occurrences, use ${parameter//pattern/string}:
message='The secret code is 12345'
echo "${message//[0-9]/X}"
# prints 'The secret code is XXXXX'
#(This is documented in the Bash Reference Manual, §3.5.3 "Shell Parameter Expansion".)