[英]Perl - Count occurrence of specific words for each line of file
做了很多搜索,没有我想要的。 Perl菜鸟在这里。
我有一个已经整齐地组织成数据行的文本文件。 假设我感兴趣的两个字符串是“ hello”和“再见”。 我想编写一个快速的Perl脚本,它将查看第一行并计算“ hello”和“再见”发生了多少次。 然后它将转到下一行并进行计数,并添加到较早的计数中。 因此,在脚本末尾,我可以打印文件中每个字符串的计数总数。 逐行方法很重要的原因是因为我想使用多个计数,所以我可以打印两个单词在同一行中的次数,一行中仅包含一个单词而不是单词的次数。另外,一行包含一次“ hello”但多次包含“ byeby”等的次数。实际上,这是关于在一行中找到每个条件的次数,而不是单词在整个文档中出现的次数。
到目前为止,我在想:
#!/usr/bin/perl
use strict; use warnings;
die etc (saving time by not including it here)
my $word_a = "hello";
my $word_b = "goodbye";
my $single_both = 0; # Number of lines where both words appear only once.
my $unique_hello = 0; # Number of lines where only hello appears, goodbye doesn't.
my $unique_goodbye = 0; # Number of lines where goodbye appears, hello doesn't.
my $one_hello_multiple_goodbye = 0; # Number of lines where hello appears once and goodbye appears multiple times.
my $one_goodbye_multiple_hello = 0; # Number of lines where goodbye appears once and hello appears multiple times.
my $multiple_both = 0; = # Number of lines where goodbye and hello appear multiple times.
while (my $line = <>) {
Magic happens here
};
# then the results for each of those variables can be printed at the end.
正如我所说,我是菜鸟。 我对如何计算每一行中的出现次数感到困惑。 即使我知道我确定自己也会确定上面列出的所有不同条件。 我应该使用数组吗? 散列? 还是考虑到我想要的东西,我完全朝错误的方向走了。 我需要计算在这些变量之后具有作为注释列出的不同条件的行数。 任何帮助都将不胜感激!
您可以通过正则表达式计算某个单词的出现次数,例如$hello = () = $line =~ /hello/g;
计算$line
hello
发生情况。 如何工作?
perl -n -E '$hello = () = /hello/g; $goodbye = () = /goodbye/g; say "line $.: hello - $hello, goodbye - $goodbye"; $hello_total += $hello; $goodbye_total += $goodbye;}{say "total: hello - $hello_total, goodbye - $goodbye_total";' input.txt
某些文件的输出:
line 1: hello - 0, goodbye - 0
line 2: hello - 1, goodbye - 0
line 3: hello - 1, goodbye - 1
line 4: hello - 3, goodbye - 0
line 5: hello - 0, goodbye - 0
line 6: hello - 1, goodbye - 1
line 7: hello - 0, goodbye - 0
total: hello - 6, goodbye - 2
Perl有一个绑定运算符=~
,用于测试字符串是否与模式匹配。 您可以将其与两个if语句结合使用,以从所有行中提取计数:
# only gathers counts
while (my $line = <STDIN>) {
$hello_cnt++ if $line =~ /hello/;
$goobye_cnt++ if $line =~ /goodbye/;
}
但是似乎您想逐行推理输入,并且可以维护所有这些变量: $unique_hello
, $unique_goodbye
等...但是这对我来说似乎是很多额外的工作,您可以做什么对所有计数进行哈希处理:
my %seen;
while (my $line = <STDIN>) {
chomp $line; # remove trailing \n
map {
$seen{lc $_}++;
} split /\s+/, $line; # split on whitespace
}
现在,您具有以下结构的哈希值:
{
word1 => cnt1,
word2 => cnt2,
etc ...
}
现在,您可以打印总计:
print "Hello seen " . $seen{hello} . " times";
# etc ...
我为您做了逐行分析,希望这是一个很好的起点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.