这是一个类似的问题,但会计算整个文件中的匹配项:
Is there a Perl shortcut to count the number of matches in a string?
相反,我希望在bash上使用perl逐行计算匹配数。
例子: ~/test.txt
~/test.txt
one.two.three.four one.two.three.four one.two.three.four one.two.three.four
这是我使用的bash命令行:
perl -0777snE 'my @count = /\./gm; say scalar @count' -- ~/test.txt
返回内容:
12
相反,我希望它逐行计数并返回给我:
3 3 3 3
我怎样才能得到这个?
简单:
$ perl -nE 'my @count = /\./gm; say scalar @count' file 3 3 3 3
不要使用 -0777 如果你不需要它。
-0777
如果你需要它:
perl -0777 -nE 'do{ my @count = /\./gm; say scalar @count } for split /\n/' file