将两个文件中的两个列表连接到一个文件中

时间:2015-10-05 10:21:44

标签: bash while-loop readline

我有2个列表,我尝试使用bash脚本组合/加入/合并。

清单1

file1-1
file1-2
file1-3
file1-4

等等

列表2包含

file2-1
file2-2
file2-3
file2-4

等等

我试图获得类似这样的输出

file1-1:file2-1
file1-2:file2-1
file1-3:file2-1
file1-4:file2-1
file1-1:file2-2
file1-2:file2-2
file1-3:file2-2
file1-4:file2-2
file1-1:file2-3
file1-2:file2-3
file1-3:file2-3
file1-4:file2-3

等等

4 个答案:

答案 0 :(得分:4)

<强> merge.sh

nil

<强>输出

var temp = queryObj["CurrentTemperature"];

答案 1 :(得分:0)

这可能是GNU Parallel

parallel -k -a file1 -a file2 echo

file1-1 file2-1
file1-1 file2-2
file1-1 file2-3
file1-1 file2-4
file1-2 file2-1
file1-2 file2-2
file1-2 file2-3
file1-2 file2-4
file1-3 file2-1
file1-3 file2-2
file1-3 file2-3
file1-3 file2-4
file1-4 file2-1
file1-4 file2-2
file1-4 file2-3
file1-4 file2-4

或者这个:

parallel -k -a file2 -a file1 echo {2}:{1}

file1-1:file2-1
file1-2:file2-1
file1-3:file2-1
file1-4:file2-1
file1-1:file2-2
file1-2:file2-2
file1-3:file2-2
file1-4:file2-2
file1-1:file2-3
file1-2:file2-3
file1-3:file2-3
file1-4:file2-3
file1-1:file2-4
file1-2:file2-4
file1-3:file2-4
file1-4:file2-4

答案 2 :(得分:0)

加入假场并随后修剪

join -t: <(sed 's/^/:/' file1) <(sed 's/^/:/' file2) | cut -c2-

答案 3 :(得分:0)

此替代方案使用awk

awk 'BEGIN {OFS=":"}
     { 
        while(getline line < "file1"){  print line,$0  }
     } 
     close("file1")
' file2