行数が異なる2つの異なるファイルをマージします。

行数が異なる2つの異なるファイルをマージします。

2つの巨大なファイルがあり、2つのファイルの行数が等しくありません。

ファイル1

61346877
41724134
85406965
59647779
25199749
86213
45417131
41905714
19415458
1828594
56543876
70603415

ファイル2

212
1231
324234
213

希望の出力

61346877,212
41724134,1231
85406965,324234
59647779,213
25199749,212
86213,1231
45417131,324234
41905714,213
19415458,212
1828594,1231
56543876,324234
70603415,213

ベストアンサー1

大きな打撃:

size1=$( wc -l < file1 )
size2=$( wc -l < file2 )
i=0
while (( i < size1 )); do
    cat file2
    (( i += size2 ))
done | paste -d, file1 -  | head -n $size1

headfile1のサイズがfile2の偶数倍数でない場合は、入力をパイプします。

出力

61346877,212
41724134,1231
85406965,324234
59647779,213
25199749,212
86213,1231
45417131,324234
41905714,213
19415458,212
1828594,1231
56543876,324234
70603415,213

3行のawkプログラム

awk -v OFS=, '
    # read the smaller file into memory
    NR == FNR {size2++; file2[FNR] = $0; next} 

    # store the last line of the array as the zero-th element
    FNR == 1 && NR > 1 {file2[0] = file2[size2]} 

    # print the current line of file1 and the corresponding file2 line
    {print $0, file2[FNR % size2]}
' file2 file1

おすすめ記事