'Replace one substring for another string in shell script
I have "I love Suzi and Marry" and I want to change "Suzi" to "Sara".
#!/bin/bash
firstString="I love Suzi and Marry"
secondString="Sara"
# do something...
The result must be like this:
firstString="I love Sara and Marry"
Solution 1:[1]
This can be done entirely with bash string manipulation:
first="I love Suzy and Mary"
second="Sara"
first=${first/Suzy/$second}
That will replace only the first occurrence; to replace them all, double the first slash:
first="Suzy, Suzy, Suzy"
second="Sara"
first=${first//Suzy/$second}
# first is now "Sara, Sara, Sara"
Solution 2:[2]
For Dash all previous posts aren't working
The POSIX sh compatible solution is:
result=$(echo "$firstString" | sed "s/Suzi/$secondString/")
This will replace the first occurrence on each line of input. Add a /g flag to replace all occurrences:
result=$(echo "$firstString" | sed "s/Suzi/$secondString/g")
Solution 3:[3]
Try this:
sed "s/Suzi/$secondString/g" <<<"$firstString"
Solution 4:[4]
It's better to use Bash than sed if strings have regular expression characters.
echo ${first_string/Suzi/$second_string}
It's portable to Windows and works with at least as old as Bash 3.1.
To show you don't need to worry much about escaping, let's turn this:
/home/name/foo/bar
Into this:
~/foo/bar
But only if /home/name is in the beginning. We don't need sed!
Given that Bash gives us magic variables $PWD and $HOME, we can:
echo "${PWD/#$HOME/\~}"
Thanks for Mark Haferkamp in the comments for the note on quoting/escaping ~.*
Note how the variable $HOME contains slashes, but this didn't break anything.
Further reading: Advanced Bash-Scripting Guide.
If using sed is a must, be sure to escape every character.
Solution 5:[5]
If tomorrow you decide you don't love Marry either she can be replaced as well:
today=$(</tmp/lovers.txt)
tomorrow="${today//Suzi/Sara}"
echo "${tomorrow//Marry/Jesica}" > /tmp/lovers.txt
There must be 50 ways to leave your lover.
Solution 6:[6]
echo [string] | sed "s/[original]/[target]/g"
- "s" means "substitute"
- "g" means "global, all matching occurrences"
Solution 7:[7]
Since I can't add a comment. @ruaka To make the example more readable write it like this
full_string="I love Suzy and Mary"
search_string="Suzy"
replace_string="Sara"
my_string=${full_string/$search_string/$replace_string}
or
my_string=${full_string/Suzy/Sarah}
Solution 8:[8]
Using AWK:
firstString="I love Suzi and Marry"
echo "$firstString" | awk '{gsub("Suzi","Sara"); print}'
Solution 9:[9]
Pure POSIX shell method, which unlike Roman Kazanovskyi's sed-based answer needs no external tools, just the shell's own native parameter expansions. Note that long file names are minimized so the code fits better on one line:
f="I love Suzi and Marry"
s=Sara
t=Suzi
[ "${f%$t*}" != "$f" ] && f="${f%$t*}$s${f#*$t}"
echo "$f"
Output:
I love Sara and Marry
How it works:
Remove Smallest Suffix Pattern.
"${f%$t*}"returns "I love" if the suffix$t"Suzi*" is in$f"I loveSuzi and Marry".But if
t=Zelda, then"${f%$t*}"deletes nothing, and returns the whole string "I love Suzi and Marry".This is used to test if
$tis in$fwith[ "${f%$t*}" != "$f" ]which will evaluate to true if the$fstring contains "Suzi*" and false if not.If the test returns true, construct the desired string using Remove Smallest Suffix Pattern
${f%$t*}"I love" and Remove Smallest Prefix Pattern${f#*$t}"and Marry", with the 2nd string$s"Sara" in between.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
