Linux/Unix Replacing a pattern in a string and saving to a new file with sed

design-patternslinuxsed

I have a task, to replace specific pattern in a string.
So far I tried commands like sed -e 's/text_to_find/text_to_replace/g' file
but I don't why it changed all string, not just a part which I wanted to change.

what I want to do is in every string that contains word china to add this Tomas_proxy.lt

To make it very clear, what I am looking for, there is file I am using:

987173,businesswirechina.com
988254,chinacfa.com
988808,1012china.com
989146,chinawise.ru
989561,chinaretailnews.com
989817,mobileinchina.cn
990894,cmt-china.com.cn
990965,chinajoy.net
992753,octaviachina.com
993238,chinadftzalex.com
993447,china-kena.com

And this is I would like to see in a new file

987173,Tomas_proxy.lt/businesswirechina.com
988254,Tomas_proxy.lt/chinacfa.com
988808,Tomas_proxy.lt/1012china.com
989146,Tomas_proxy.lt/chinawise.ru
989561,Tomas_proxy.lt/chinaretailnews.com
989817,Tomas_proxy.lt/mobileinchina.cn
990894,Tomas_proxy.lt/cmt-china.com.cn
990965,Tomas_proxy.lt/chinajoy.net
992753,Tomas_proxy.lt/octaviachina.com
993238,Tomas_proxy.lt/chinadftzalex.com
993447,Tomas_proxy.lt/china-kena.com

P.s. This is just example file, In real file I am using, not every line has word china ,there is 100000 strings and lets say about 500 has china

Best Solution

You can try this sed command

sed 's/,\(.*china\)/,Tomas_proxy.lt\/\1/' FileName

or

sed 's/,\(.*china\)/,Tomas_proxy.lt\/\1/' FileName > NewFile

or

sed  -i.bak 's/,\(.*china\)/,Tomas_proxy.lt\/\1/' FileName 
Related Question