无法通过Zsh / AWK组合文件中的单词

时间:2009-04-29 19:08:09

标签: awk zsh

我有File1

A,B,C

和File2

D,E,F

我想要

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

未成功

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

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

如何通过Zsh / AWK解决问题?

2 个答案:

答案 0 :(得分:2)

我不知道zsh,这是我用bash和sed做的事情:

echo "A,B,C" >a
echo "D,E,F" >b
for i in `cat a | sed -e "s@,@\n@g"`; 
      do for j in `cat b | sed -e "s@,@\n@g"`; 
      do echo -n "$i$j, "; 
      done ; 
      done | sed -e "s@,\s\$@@"

输出是:

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

答案 1 :(得分: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