2012-09-28 47 views
0

我想用我的awk腳本獲得的值在Perl中創建一個數組。然後我可以在Perl中對它們進行數學運算。管道輸出從awk到perl

這裏是我的Perl,它運行一個程序,從而節省了一個文本文件:

my $unix_command_dsc = (`./program -s test.fasta saved_file.txt`); 
my $dsc_run = qx($unix_command_dsc); 

現在我有一些在awk,它分析的是保存在文本文件中的數據:

#!/usr/bin/awk -f 

BEGIN{  # Initialize the values to zero. Note, done automatically also. 
    sumc4 = 0 
    sumc5 = 0 
    sumc6 = 0 
} 

/^[1-9][0-9]* residue/ {next} #Match line that begins with number and has word 'residue', skip it. 
/^[1-9]/ {      #Match line that begins with number. 

    sumc4 += $4     #Add up the values of the nth column into the variables. 
    sumc5 += $5 
    sumc6 += $6 

    print $4 "\t" $5 "\t" $6 #This will show the whole columns. 

    } 

END{ 
    print "sum H" "\t" "sum E" "\t" "sum C" 
    print sumc4 "\t" sumc5 "\t" sumc6 
} 

我從終端用下面的命令運行這個awk中:

./awk_program.txt saved_file.txt 

任何想法我怎麼會從p收集該數據awk中的rint語句插入到perl中的數組中?

我已經試過是剛剛運行awk腳本在Perl:

my $unix_command_awk = (`./awk_program.txt saved_file.txt`); 
my $awk_run = qx($unix_command_awk); 

但perl的給我的錯誤,命令沒有找到,喜歡它認爲數據是命令。應該在awk中存在一個我缺少的STDOUT,而不是打印?

+0

那豈不是更簡單,只是重新實現在Perl的邏輯。它將大致採用與awk代碼相同數量的代碼,並且您不需要調用外部進程並在之後解析結果。 – pmakholm

+0

所以我仍然必須解析結果,即使我在awk中這樣做?是否有辦法在awk [value1,value2 ...]中創建一個列表,然後能夠在perl中使用該列表? – chimpsarehungry

回答

3

應該僅僅是:

my $awk_run = `./awk_program.txt saved_file.txt`; 

反引號告訴Perl來運行該命令並返回輸出。因此,您對$ unix_command_awk的分配正在運行該命令,然後qx($unix_command_awk)將執行輸出作爲新命令。從AWK

+0

是的,我只是$ awk_run @array變量,我得到了我想要的。 – chimpsarehungry

1

管到你的perl腳本:

./awk_program file.txt | perl perl-script.pl 

然後從標準輸入perl的讀取裏面:

while (<>) { 
    # do stuff with $_ 
    my @cols = split(/\t/); 
} 
+0

對不起,在我的手機上編碼很難。我會編輯。 – RobEarl

+0

我得到sh:fork:資源暫時不可用 – chimpsarehungry