@ARGV:perl命令行參數

 當perl腳本運行時,從命令行上傳遞給它的參數存儲在內建數組@ARGV中,@ARGV是PERL默認用來接收參數的數組,可以有多個參數,$ARGV[0]是表示接收到的第一個參數,$ARGV[1]表示第二個。
使用方法爲:
perl   my.pl $ARGV[0]  $ARGV[1]
看一個具體例子:
比如文件1的內容:
1320238
1320239
1320239
1320238
1320238
1320238
1320235
1320237

文件2的內容:
102 5709072117805887 4001 1301854
102 5709072117807510 4001 1320292
102 5709072117838653 4001 1301857
102 5709072117814280 4001 1305832
102 5709072117839397 4001 1310673
102 5709072117839335 4001 1311270

我想先把文件1的內容讀取出來,然後讀取文件二的內容,在讀取文件2的內容的時候,文件2的最後一列需要包含在上文件1內。
[root@localhost ~]$ perl  ex.pl 1.txt 2.txt
[root@localhost ~]$ cat ex.pl
#!/usr/bin/perl
use strict;

open(ONE,"$ARGV[0]") or die $!;
open(TWO,"$ARGV[1]") or die $!;

my %hash;
while (<TWO>) {
    chomp;
    my @line=split;
    my $column4=$line[3];
    $hash{$column4}=$_;
}

while (<ONE>) {
     chomp;
     print $hash{$_} if defined $hash{$_};
    
}

print"\n";
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章