[英]find files with more than 1 match using grep
I'm wondering if there's a way to list files with more than 1 match using grep. 我想知道是否有一种方法可以使用grep列出匹配项超过1个的文件。 Here's what i'm doing. 这就是我在做什么。 I'm trying to go through a client's website and replace the last match with a string. 我正在尝试浏览客户的网站,并用字符串替换最后一个匹配项。 so at the bottom of every page, he has this line: 所以在每一页的底部,他都有这一行:
<p align="center"><font face="Arial" color="#808080" size="2">
which is followed by a footer. 然后是页脚。 I need to replace it (using sed) with this line: 我需要用以下这一行替换它(使用sed):
<?php include 'footer.php'; ?>
but i want to isolate the files that only have 1 instance of that first line, so i can replace them. 但是我想隔离只有第一行的1个实例的文件,所以我可以替换它们。 Another solution to my problem would be to use sed on only the last match, or use sed on the last 50 lines of a file. 解决我的问题的另一种方法是仅在最后一个匹配项上使用sed,或者在文件的最后50行上使用sed。 any ideas for me? 有什么想法吗?
You can actually use grep -c
to get a count of matches in a file: 您实际上可以使用grep -c
来获取文件中的匹配计数:
grep -c '<p align="center"><font face="Arial" color="#808080" size="2">' file
To store its output 存储其输出
cnt=$(grep -c '<p align="center"><font face="Arial" color="#808080" size="2">' file)
I would use this: 我会用这个:
for a in *; do one=\`grep exit $a | wc -l\`; if [ $one = 1 ]; then echo replace in $a; fi; done
and in place of echo
, do your replacement. 并代替echo
进行替换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.