2017-04-12 20 views
-1

我有一些存儲在數組中的值如下,現在我需要對屬性值進行一些檢查,建議我如何在Perl中繼續。如何awk perl數組的值?

@ arr1 =`cat passwd.txt | tr'''\ n'| egrep -i「maxage | minage」| sort'`;

陣列ARR1含有作爲「最大生存週期= 0 MINAGE = 0」

在這方面,我需要執行如果上最大生存週期的值條件,有像下面任何方式信息,建議我如我我是perl的新手。

if (@arr1[0]|awk -F= '{print $2}' == 0) { printf "Then print task done"; }

+1

您能否給我們一些_example_數據。因爲這是一個非常糟糕的方式,所以你可以使用perl來完成整個事情。 – Sobrique

+0

請顯示'passwd.txt'文件。 –

+0

passwd.txt文件在aix操作系統中具有「#lsuser root」的輸出信息。我剛剛進入一個文件進行實驗,實際上我需要使用該命令。 –

回答

0

你可以試試這個?

@arr1 = `cat passwd.txt | tr ' ' '\n' | grep -i "maxage|minage"| sort`; 
$x = `$arr1[0] | awk -F= {print $2}`; // maybe $3 is true index 
if (x == 0) 
    { 
     print "Then print task done"; 
    } 
1

您可以在Perl中完成整個過程。例如:

use feature qw(say); 
use strict; 
use warnings; 

my $fn = 'passwd.txt'; 
open (my $fh, '<', $fn) or die "Could not open file '$fn': $!"; 
my @arr1 = sort grep /maxage|minage/i, split ' ', <$fh>; 
close $fh; 
if ((split /=/, shift @arr1)[1] == 0) { 
    say "done"; 
}