【问题标题】:How to add text at the end of a line containing a string with a pattern in a text file?如何在文本文件中包含带有模式的字符串的行尾添加文本?
【发布时间】:2016-11-04 00:19:14
【问题描述】:

例如:

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

【问题讨论】:

    标签: linux bash shell unix terminal


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      您不需要在 sed 命令中使用 glob 或其他 *。以下对我有用:

      sed -e '/hello/ s/$/ 0/' hello.txt
      

      一旦你确定它适合你,你就可以使用-i(使用 GNU)

      【讨论】:

        【解决方案3】:
        awk '/hello/{print $0" "0}' filename
        

        【讨论】:

          猜你喜欢
          • 2017-10-06
          • 1970-01-01
          • 2020-01-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-03-29
          • 1970-01-01
          • 2014-08-06
          相关资源
          最近更新 更多