Pulse Pulse - 5 months ago 8
Linux Question

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

For example:

hello1.jpg
hello2.jpg
hello3.jpg


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

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