Linux 如何在文本文件中包含带模式的字符串的行尾添加文本?

Linux 如何在文本文件中包含带模式的字符串的行尾添加文本?,linux,bash,shell,unix,terminal,Linux,Bash,Shell,Unix,Terminal,例如: hello1.jpg hello2.jpg hello3.jpg 我想对其进行编辑,使其如下所示: hello1.jpg 0 hello2.jpg 0 hello3.jpg 0 我试过: sed -i '/(hello*)/ s/$/ 0/' hello.txt perl -ipe 's/$/ 0/ if /hello/' hello.txt sed -i '/^hello*/ s/$/ 0/' hello.txt 您的第一种方法几乎是正确的,除了括号(不需要)被逐字解释之外: $

例如:

hello1.jpg
hello2.jpg
hello3.jpg
我想对其进行编辑,使其如下所示:

hello1.jpg 0
hello2.jpg 0
hello3.jpg 0
我试过:

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

您的第一种方法几乎是正确的,除了括号(不需要)被逐字解释之外:

$ sed '/hello/ s/$/ 0/' file
hello1.jpg 0
hello2.jpg 0
hello3.jpg 0

您不需要在sed命令中使用globs或其他
*
。以下是我的作品:

sed -e '/hello/ s/$/ 0/' hello.txt
一旦您确定它适合您,您就可以使用
-i
(与GNU一起使用)

awk '/hello/{print $0" "0}' filename