Pulse Pulse - 1 year ago 57
Linux Question

How to add text at the end of a line containing a string with a pattern in a text file?

For example:


I would like to edit it so that it is like this:

hello1.jpg 0
hello2.jpg 0
hello3.jpg 0

I have tried:

sed -i '/(hello*)/ s/$/ 0/' hello.txt
perl -ipe 's/$/ 0/ if /hello/' hello.txt
sed -i '/^hello*/ s/$/ 0/' hello.txt

Answer Source

Your first approach was almost correct, aside from the fact that the parentheses (which aren't needed) were being interpreted literally:

$ sed '/hello/ s/$/ 0/' file
hello1.jpg 0
hello2.jpg 0
hello3.jpg 0
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download