我有一个类似的文件:
fan apple banana orange mango
cooler apple orange banana mango
cooler apple orange banana mango
cooler apple orange banana mango
fan apple banana orange mango
fan apple banana orange mango
cooler apple orange banana mango
fan apple banana orange mango
现在,当我逐行前进的时候,我想算一下Fan来的时候,需要增加每个粉丝的数量。像
count 1
fan apple banana orange mango
cooler apple orange banana mango
cooler apple orange banana mango
cooler apple orange banana mango
count 2
fan apple banana orange mango
count 3
fan apple banana orange mango
cooler apple orange banana mango
count 4
fan apple banana orange mango
这就是我想要的输出。感谢帮助......谢谢
答案 0 :(得分:1)
#!/usr/bin/perl
use strict;
use warnings;
my $n = 0;
while (<>) {
print "count ", ++$n, "\n" if /^fan\b/;
print;
}
或使用Perl 5.10 +作为单行代码:
perl -pE 'say "count ", ++$n if /^fan\b/'
答案 1 :(得分:0)
单行:
perl -lane 'BEGIN{$c=1}; $F[0] eq "fan" && do{print "count ".$c++ }; print' file