Read variables from two files and output in a patt

2019-07-30 02:07发布

问题:

I have 2 files like below:

file1:

a1,b1,c1,d1,e1,f1,g1,h1
a2,b2,c2,d2,e2,f2,g2,h2
a3,b3,c3,d3,e3,f3,g3,h3
a4,b4,c4,d4,e4,f4,g4,h4

file2:

x1,y1,z1
x2,y2,z2
x3,y3,z3
x4,y4,z4

I want to read simultaneously from both and output the variables in a pattern like below:

a1,b1,c1,d1,x1,e1,f1,y1,g1,z1,h1
a2,b2,c2,d2,x2,e2,f2,y2,g2,z2,h2
a3,b3,c3,d3,x3,e3,f3,y3,g3,z3,h3
a4,b4,c4,d4,x4,e4,f4,y4,g4,z4,h4

Good news - I've managed to achieve it !!

Bad news - Too many arrays and while loops (too much computation!). I am looking for something simpler as the script will have to read through much data (4k lines and 1M words).

Limitation - BASH shell (probably not a limitation!)

This is what I've done

exec 5<file1 # Open file into FD 5
exec 6<file2 # Open file into FD 6

while IFS=$"," read -r line1 <&5
IFS=$"," read -r line2 <&6
do
    array1=( `echo $line1` )
    array2=( `echo $line2` )
    array3=("${array1[@]}","${array2[@]}")
    echo ${array3[@]} >> tmpline
done
while IFS="," read var1 var2 var3 var4 var5 var6 var7 var8 var9 var10 var11
do
    echo -e "$var1,$var2,$var3,$var4,$var9,$var5,$var6,$var10,$var8,$var11,$var9" >> tcomb
done < tmpline

exec 5<&- # Close FD 5
exec 6<&- # Close FD 6

Thanks in advance -- I'm waiting patiently :) !!

回答1:

Try this:

exec 5<file1 # Open file into FD 5
exec 6<file2 # Open file into FD 6

while IFS=, read -a t <&5 &&
      IFS=, read -a u <&6
do
    echo -n "${t[0]},${t[1]},${t[2]},${t[3]},${u[0]},${t[4]},"
    echo    "${t[5]},${u[1]},${t[6]},${u[2]},${t[7]}"
done >| tcomb

exec 5<&- # Close FD 5
exec 6<&- # Close FD 6


回答2:

You can use paste to combine the lines of the files. Then, you have to reorder the columns, I used Perl for that:

paste file1 file2 -d, | \
    perl -F, -ane 'chomp $F[-1]; $"=","; print "@F[0..3,8,4,5,9,6,10,7]\n"'


回答3:

If you allow yourself to read the files more than once, and using bash process substitution:

paste -d , <(cut -d , -f 1-4 file1) \
           <(cut -d , -f 1 file2) \
           <(cut -d , -f 5-6 file1) \
           <(cut -d , -f 2 file2) \
           <(cut -d , -f 7 file1) \
           <(cut -d , -f 3 file2) \
           <(cut -d , -f 8 file1)