1

我有一个像这样的文件:

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

现在,当我逐行进行时,我想在范来的那一刻数一数,并且需要增加每个粉丝的计数。喜欢

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

这就是我想要的输出。帮助表示赞赏...谢谢

4

2 回答 2

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/'
于 2013-04-05T21:58:14.577 回答
0

在单行中:

perl -lane 'BEGIN{$c=1}; $F[0] eq "fan" && do{print "count ".$c++ }; print' file
于 2013-04-05T22:01:06.543 回答