Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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
share|improve this question

2 Answers 2

up vote 0 down vote accepted

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

share|improve this answer
    
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 –  Sajith4U Jul 16 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 at 9:04
    
Thank you so so much you saved my time. –  Sajith4U Jul 16 at 9:10
    
No big deal, just learn the syntax for the next time ;) –  Pabi Jul 16 at 9:14

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.

share|improve this answer
    
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> –  Sajith4U Jul 16 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 –  Sajith4U Jul 16 at 8:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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