从两个文件中读取变量,并在第三个文件中以模式输出 [英] Read variables from two files and output in a pattern on 3rd file

查看:73
本文介绍了从两个文件中读取变量,并在第三个文件中以模式输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个文件,如下所示:

I have 2 files like below:

文件1:

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

文件2:

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 !!

坏消息-太多的数组和while循环(太多的计算!).我正在寻找更简单的方法,因为脚本必须读取大量数据(4k行和1M字).

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).

限制-BASH外壳(可能不是一个限制!)

Limitation - BASH shell (probably not a limitation!)

这就是我所做的

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 :) !!

推荐答案

尝试一下:

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

这篇关于从两个文件中读取变量,并在第三个文件中以模式输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆