tags:

views:

91

answers:

2

I have File1

A,B,C

and File2

D,E,F

I am trying to have

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

unsuccessfully by

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

giving

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

How can you solve the problem by Zsh/AWK?

+2  A: 

I don't know zsh, here's what I did with bash and 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\$@@"

The output then is:

AD, AE, AF, BD, BE, BF, CD, CE, CF
lothar
+2  A: 
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

If File1 contains

A,B,C
1,2,3

and File2 contains

D,E,F
4,5,6

then the awk script outputs

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