2012-10-22 29 views
0

我有一個awk的代碼合併2個文件,並將結果添加到使用file.txt的結束「>>」放AWK代碼和排序結果

我的代碼

NR==FNR && $2!=0 {two[$0]++;j=1; next }{for(i in two) {split(i,one,FS); if(one[3] == $NF){x=$4;sub(/[[:digit:]]/, "A", $4); print j++,$1,$2,$3,x,$4 | "column -t" ">>" "./Desktop/file.txt"}}} 

我希望把我的awk來bash腳本和finaly排序我的file.txt的,並保存排序結果,再次使用FILE.TXT >

我想這

#!/bin/bash 
command=$(awk '{NR==FNR && $2!=0 {two[$0]++;j=1; next }{for(i in two) {split(i,one,FS); if(one[3] == $NF){x=$4;sub(/[[:digit:]]/, "A", $4); print $1,$2,$3,$4 | "column -t" ">>" "./Desktop/file.txt"}}}}') 
echo -e "$command" | column -t | sort -s -n -k4 > ./Desktop/file.txt 

但它給我錯誤"for reading (no such a file or directory)"

我的錯誤在哪裏?

在此先感謝

+0

你能舉出輸入文件和預期結果的例子嗎? –

回答

1

1)您沒有指定awk腳本的輸入文件。這:

command=$(awk '{...stuff...}') 

需求是:

command=$(awk '{...stuff...}' file1 file2) 

2),再移動awk的條件 「NR == ...」 動作部分內,因此將不再表現爲條件。 3)你的awk腳本輸出進入「file.txt」,所以當你在後續行上回顯它時,「command」是空的。

4)您有未使用的變量X和j

5)你傳遞ARG FS分裂()不必要地。

等等

我想你想要的是:

command=$(awk ' 
    NR==FNR && $2!=0 { two[$0]++; next } 
    { 
     for(i in two) { 
      split(i,one) 
      if(one[3] == $NF) { 
      sub(/[[:digit:]]/, "A", $4) 
      print $1,$2,$3,$4 
      } 
     } 
    } 
' file1 file2) 
echo -e "$command" | column -t >> "./Desktop/file.txt" 
echo -e "$command" | column -t | sort -s -n -k4 >> ./Desktop/file.txt 

,但很難說。