Replace a word with another in bash
You can use sed for that:
$ sed s/sara/mary/g <<< 'hello sara , my name is sara too .'
hello mary , my name is mary too .
Or if you want to change a file in place:
$ cat FILE
hello sara , my name is sara too .
$ sed -i s/sara/mary/g FILE
$ cat FILE
hello mary , my name is mary too .
You can use sed:
# sed 's/sara/mary/g' FILENAME
will output the results. The s/// construct means search and replace using regular expressions. The 'g' at the end means "every instance" (not just the first).
You can also use perl and edit the file in place:
# perl -p -i -e 's/sara/mary/g;' FILENAME
Or awk
awk '{gsub("sara","mary")}1' <<< "hello sara, my name is sara too."
Pure bash way:
before='hello sara , my name is sara too .'
after="${before//sara/mary}"
echo "$after"
OR using sed:
after=$(sed 's/sara/mary/g' <<< "$before")
echo "$after"
OUTPUT:
hello mary , my name is mary too .