我如何计数整个文件字符串出现的次数? [英] How do I count the number of occurrences of a string in an entire file?

查看:110
本文介绍了我如何计数整个文件字符串出现的次数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个内置的命令,要做到这一点,或有没有人有一个脚本,做任何运气?

Is there an inbuilt command to do this or has anyone had any luck with a script that does it?

我期待指望某些字符串(不字)出现在文件的次数。这可以包括每行多次出现这样的计算应该每次发生不只是计数1具有字符串2倍以上线。

I am looking to count the number of times a certain string (not word) appears in a file. This can include multiple occurrences per line so the count should count every occurrence not just count 1 for lines that have the string 2 or more times.

例如,这个示例文件:

blah(*)wasp( *)jkdjs(*)kdfks(l*)ffks(dl
flksj(*)gjkd(*
)jfhk(*)fj (*) ks)(*gfjk(*)

如果我期待数字符串出现(*)我预计数为6,即2从第二个第一线,1从第三行线和3。注意如何在2-3线的一个不计,因为有一个LF字符分隔。

If I am looking to count the occurrences of the string (*) I would expect the count to be 6, i.e. 2 from the first line, 1 from the second line and 3 from the third line. Note how the one across lines 2-3 does not count because there is a LF character separating them.

更新:伟大的答复迄今!我能问脚本处理转换(*) \\(* \\),等?这样,我可以通过任何所需的字符串作为输入参数,而不用担心转换需要做它,因此它出现在正确的格式。

Update: great responses so far! Can I ask that the script handle the conversion of (*) to \(*\), etc? That way I could just pass any desired string as an input parameter without worrying about what conversion needs to be done to it so it appears in the correct format.

推荐答案

使用perl的爱斯基摩吻运营商与 -n 开关打印总在最后。使用 \\ Q ... \\ E 忽略任何元字符。

Using perl's "Eskimo kiss" operator with the -n switch to print a total at the end. Use \Q...\E to ignore any meta characters.

perl -lnwe '$a+=()=/\Q(*)/g; }{ print $a;' file.txt

脚本:

use strict;
use warnings;

my $count;
my $text = shift;

while (<>) {
    $count += () = /\Q$text/g;
}

print "$count\n";

用法:

perl script.pl "(*)" file.txt 

这篇关于我如何计数整个文件字符串出现的次数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆