bash - 从 grep 打印行号,用逗号分隔

标签 bash shell grep cut

我使用 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

最佳答案

使用:

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

关于bash - 从 grep 打印行号,用逗号分隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24268709/

相关文章:

linux - 将所有 bash 输出传输到脚本?

bash - 男人 grep | grep "color"没有给出结果

linux - bash 中的 If then elif then else 语句

regex - Linux:如何仅使用 ls 列出所有文件/目录

regex - Grep:无效的重复计数

linux - 如何在 .bashrc 中创建一个函数,当函数运行的命令中有空格时,该函数将正常工作?

shell - 执行 KornShell 脚本

linux - 脚本 : Is it possible to format seq to display n numbers per line?

linux - 查看当前存在多少 SSH 连接

python - 使用 grep 比较两个 Python 包列表