1

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
0

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

  • hi this is the one i want to change.<app.lib>D:\\app\\lib</app.lib>. to <app.lib>D:\\app2\\lib</app.lib> How i want to change.i will appreciate your he – Sajith Vijesekara Jul 16 '14 at 8:55
  • sed -i 's/<app.lib>D:\\\\app\\\\lib<\/app.lib>/<app.lib>D:\\\\app2\\\\lib<\/app.lib>/g' yourfile – Pabi Jul 16 '14 at 9:04
  • No big deal, just learn the syntax for the next time ;) – Pabi Jul 16 '14 at 9:14
0

Assuming your file is YOURFILE in the current directory, try sed -i 's/8080/3128/g' YOURFILE

This will replace all occurences of 8080 with 3128.

  • Thanks how we replace sentence using sed.like this one i have another chjange for change path in xml.<path>home/user/location</path> to <path>/user/tmp/location2</path> – Sajith Vijesekara Jul 16 '14 at 8:45
  • hi this is the one i want to change.<app.lib>D:\\app\\lib</app.lib>. How i want to change.i will appreciate your help – Sajith Vijesekara Jul 16 '14 at 8:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.