2009-04-29 38 views
0

單詞中的文件結合我有文件1無法被Zsh/AWK

A,B,C 

和File2

D,E,F 

我試圖通過

echo {`cat File1`}{`cat File2`} 

AD, AE, AF, BD, BE, BF, CD, CE, CF 

失敗

{A,B,C}{D,E,F} 

你怎麼可能被Zsh/AWK解決這個問題?

回答

2
awk -F, ' 
    NR==FNR { 
     # read lines from File1 into the array f1 
     f1[NR]=$0 
     next 
    } 
    { 
     # foreach line in File2 
     split(f1[FNR], words); # get words from corresponding line in File1 
     sep = "" 
     for (i in words) { 
      for (j=1; j<=NF; j++) { 
       printf("%s%s%s", sep, words[i], $j) 
       sep = ", " 
      } 
     } 
     print "" 
    } 
' File1 File2 

如果file1包含

A,B,C 
1,2,3 

和File2包含

D,E,F 
4,5,6 

則AWK腳本輸出

AD, AE, AF, BD, BE, BF, CD, CE, CF 
14, 15, 16, 24, 25, 26, 34, 35, 36