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

concatenate two lists from two files into one file

我有 2 个列表,我正在尝试 combine/join/merge 使用 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

等等

merge.sh

#!/bin/bash

while read line2; do
  while read line1; do
    printf "$line1:$line2\n";
  done < file1.txt;
done < file2.txt;

输出

$ ./merge.sh 
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

也许这与 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

加入一个假的领域,然后 trim 它

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

此备选方案使用 awk:

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