【问题标题】:Print line numbers from grep separated with a comma从 grep 打印行号,用逗号分隔
【发布时间】:2014-08-07 18:07:04
【问题描述】:

我使用 grep 从出现特定字符串的文件中获取行号。如果我希望这些行号出现在由逗号分隔的行中而不是垂直行中怎么办?

cFont=$(grep -icE "<font *" $htmlFile)
if [ $cFont -gt 0 ] ; then
  echo "There are $cFont line(s) with <font> tag, they are: " >> ~/Desktop/errors.txt
  grep -inE "<font *" $htmlFile |cut -f1 -d: >> ~/Desktop/errors.txt
fi

结果是

There are 4 line(s) with <font> tag, they are: 
14
43
46
72

我希望它是

There are 4 line(s) with <font> tag, they are: 
14, 43, 46, 72

【问题讨论】:

    标签: bash shell grep cut


    【解决方案1】:

    用途:

    grep -inE "<font *" $htmlFile |cut -f1 -d:| tr '\n' ','|sed -e "s/,$//" >> ~/Desktop/errors.tr
    

    【讨论】:

      【解决方案2】:

      您可以使用 awk 代替 cut -f1 -d:

      awk -F: '{printf "%s, ", $1} END {print ""}'
      

      【讨论】:

      • $1会不会改变我原来设置的环境变量??
      • 不,因为$1 是 awk 内部的
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-27
      • 2021-07-20
      • 2017-12-19
      • 2021-05-10
      相关资源
      最近更新 更多