通过在perl中包含一个衬垫来删除前两行

时间:2014-12-23 09:52:18

标签: perl

我将以下文件作为脚本的输出: -

----Local Rates----

Market Merchant SE number mismatch Dupe Check S.Number Local Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec FY
Abc Asfinag  1 9406589846 EUR 1,125 1,125 1,125 1,125 1,125 1,125 1,125 1,125 1,125 1,125 1,125 1,125 13,500
Abc BÖG  1 #N/A! EUR 844 844 844 844 844 844 844 844 844 844 844 844 10,125
Abc Gault Millau  1 9496732942 EUR 3,375 3,375 3,375 3,375 3,375 3,375 3,375 3,375 3,375 3,375 3,375 3,375 40,500
Abc OEGV  1 9404123606 EUR 187 187 187 187 187 187 187 187 187 187 187 187 2,244
Abc Relax Guide  1 9404130395 EUR 675 675 675 675 675 675 675 675 675 675 675 675 8,100
Abc Sport 2000  1 9495264095 EUR 51 51 51 51 51 51 51 51 51 51 51 51 608
Abc Strategic Investment Pool  1 #N/A! EUR 1,980 1,980 1,980 1,980 1,980 1,980 1,980 1,980 1,980 1,980 1,980 1,980 23,760
Abc Strolz  1 #N/A! EUR 281 281 281 281 281 281 281 281 281 281 281 281 3,375
Abc Urlaubsspezialisten  1 Urlaubsspezialisten EUR 619 619 619 619 619 619 619 619 619 619 619 619 7,425
Abc Vamed  1 9404119554 EUR 281 281 281 281 281 281 281 281 281 281 281 281 3,375
Abc Vienna Fashion Night  1 Vienna Fashion Night EUR 1,688 1,688 1,688 1,688 1,688 1,688 1,688 1,688 1,688 1,688 1,688 1,688 20,250
Abc Vineus / C&C Pfeifer  1 Vineus / C&C Pfeifer EUR 1,012 1,012 1,012 1,012 1,012 1,012 1,012 1,012 1,012 1,012 1,012 1,012 12,150
Abc IKEA  1 #N/A! EUR 394 394 394 394 394 394 394 394 394 394 394 394 4,725
Abc REWE  1 9502932411 EUR 675 675 675 675 675 675 675 675 675 675 675 675 8,100

我使用以下代码获得了上述输出:

#!/usr/bin/perl

use strict;
use warnings;

open my $fh, '<', 'File' or die "Can't open file: $!\n";

while (<$fh>) {
    my @fields = split(/\t/);
    print "@fields[0,1,3,4,5,6,21,22,23,24,25,26,27,28,29,30,31,32,33]\n";
}

现在我试图删除前两行,所以我想在脚本中使用下面一行,但我无法合并它。

perl -i.bak -ne '$.>=4 && print' file

我该怎么办?

3 个答案:

答案 0 :(得分:3)

你不需要外部一个班轮就可以跳过前两行,

while (<$fh>) {
  next if $. <= 2;

  # ..
}

答案 1 :(得分:2)

尝试这样做,逻辑刚刚逆转,我只是添加了行

$. <= 2 and next;

到你自己的代码:

#!/usr/bin/perl

use strict;
use warnings;

open my $fh, '<', 'File' or die "Can't open file: $!\n";

while (<$fh>) {
    $. <= 2 and next;
    my @fields = split(/\t/);
    print "@fields[0,1,3,4,5,6,21,22,23,24,25,26,27,28,29,30,31,32,33]\n";
}

答案 2 :(得分:0)

你可以通过在印刷品中使用if条件来实现同样的目的,即如果$。 &GT; 2。

#!/usr/bin/perl
use strict;
use warnings;
open my $fh, '<', 'File' or die "Can't open file: $!\n";
while (<$fh>) {
    my @fields = split(/\t/);
    print "@fields[0,1,3,4,5,6,21,22,23,24,25,26,27,28,29,30,31,32,33]\n" if $. > 2;
}

#!/usr/bin/perl use strict; use warnings; open my $fh, '<', 'File' or die "Can't open file: $!\n"; while (<$fh>) { my @fields = split(/\t/); print "@fields[0,1,3,4,5,6,21,22,23,24,25,26,27,28,29,30,31,32,33]\n" if $. > 2; }