使用 grep 查找具有超过 1 个匹配项的文件

find files with more than 1 match using grep

我想知道是否有一种方法可以使用 grep 列出具有超过 1 个匹配项的文件。这就是我在做什么。我正在尝试浏览客户的网站并用字符串替换最后一个匹配项。所以在每一页的底部,他都有这样一行:

<p align="center"><font face="Arial" color="#808080" size="2">

后面是页脚。我需要用这一行替换它(使用 sed):

<?php include 'footer.php'; ?>

但我想隔离只有第一行的一个实例的文件,所以我可以替换它们。我的问题的另一种解决方案是仅对最后一场比赛使用 sed,或对文件的最后 50 行使用 sed。对我有什么想法吗?

我会用这个:

for a in *; do one=\`grep exit $a | wc -l\`; if [ $one = 1 ]; then echo replace in $a; fi; done

代替 echo,进行替换。

您实际上可以使用 grep -c 来获取文件中匹配项的计数:

grep -c '<p align="center"><font face="Arial" color="#808080" size="2">' file

存储它的输出

cnt=$(grep -c '<p align="center"><font face="Arial" color="#808080" size="2">' file)