cat command replace text Linux shell script
I have shell script file. In there is code like: <tag>port:8080</tag>.
I want to replace this code line with another code line: <tag>port:3128</tag>.
How do I do that?
I used cat, but it removed all the code lines and only added this code line..can anyone tell me how to replace the text in shell scripting ?
This is how I tryed it:
cat <<EOF > /home/samples/pom.xml
<tag>port:3128</tag>
EOF 2 Answers
The general command is sed -i 's/old/new/g' yourfile.
But you have to escape special characters with \.
So the command is: sed -i 's/<tag>port:8080<\/tag>/<tag>port:3128<\/tag>/g' /home/samples/pom.xml
For the path it is::sed -i 's/<path>home\/user\/location<\/path>/<path>\/user\/tmp\/location2<\/path>/g' /path/to/file
Assuming your file is YOURFILE in the current directory, trysed -i 's/8080/3128/g' YOURFILE
This will replace all occurences of 8080 with 3128.
2