如何合并唯一值和重复列

时间:2016-04-26 08:45:50

标签: linux awk

我有两个文件,如下所述格式我想根据文件1列2和文件2 column1选择值,并将文件1中的值分配给文件2.

文件1:

1001    2487    373256      2060936     4.44322
1002    2488    373266.89   2060942.14  4.44315
1003    2490    373288.67   2060954.43  4.44286
1004    2492    373310.45   2060966.71  4.44261
1005    2494    373332.23   2060978.99  4.44272
1006    2496    373354.02   2060991.28  4.44265
1007    2498    373375.8    2061003.56  4.44221
1008    2500    373397.58   2061015.85  4.44186
1009    2502    373419.36   2061028.13  4.44181
1010    2504    373441.14   2061040.42  4.44186
1011    2506    373462.92   2061052.7   4.44183
1012    2508    373484.7    2061064.98  4.44181
1013    2510    373506.49   2061077.27  4.44169
1014    2512    373528.27   2061089.55  4.44169
1015    2514    373550.05   2061101.84  4.44168
1016    2516    373571.83   2061114.12  4.44163
1017    2518    373593.61   2061126.4   4.44163
1018    2520    373615.39   2061138.69  4.44156
1019    2522    373637.17   2061150.97  4.44156
1020    2524    373658.96   2061163.26  4.44158
1021    2526    373680.74   2061175.54  4.44153
1022    2528    373702.52   2061187.83  4.44141
1023    2530    373724.3    2061200.11  4.44131
1024    2532    373746.08   2061212.39  4.44126
1025    2534    373767.86   2061224.68  4.44122
1026    2536    373789.64   2061236.96  4.44119
1027    2538    373811.43   2061249.25  4.44112
1028    2540    373833.21   2061261.53  4.44107
1029    2542    373854.99   2061273.81  4.44107
1030    2544    373876.77   2061286.1   4.44107
1031    2546    373898.55   2061298.38  4.44105
1032    2548    373920.33   2061310.67  4.44104
1033    2550    373942.11   2061322.95  4.44103
1034    2552    373963.9    2061335.23  4.44103
1035    2554    373985.68   2061347.52  4.44099
1036    2556    374007.46   2061359.8   4.44094
1037    2558    374029.24   2061372.09  4.44085
1038    2560    374051.02   2061384.37  4.44077

文件2:

2500    4332    1497.9
2500    4788    1527.3
2500    5252    1586
2500    5788    1703.5
2500    6456    1899.4
2500    7132    2124.6
2500    8184    2496.6
2580    4332    1497.9
2580    4736    1507.7
2580    5212    1576.2
2580    5872    1713.3
2580    6348    1879.8
2580    6944    2095.2
2580    8184    2496.6
2660    4432    1499.5
2660    4736    1507.7
2660    5192    1576.2
2660    5860    1713.3
2660    6368    1870
2660    6860    2046.2
2660    8184    2496.6
2740    4416    1501.6
2740    4680    1517.5
2740    4936    1537.1
2740    5176    1566.5
2740    5584    1654.6
2740    5888    1723.1
2740    6408    1879.8
2740    7068    2085.4

输出格式:

373397.58   2061015.85  2500    4332    1497.9
373397.58   2061015.85  2500    4788    1527.3
373397.58   2061015.85  2500    5252    1586
373397.58   2061015.85  2500    5788    1703.5
373397.58   2061015.85  2500    6456    1899.4
373397.58   2061015.85  2500    7132    2124.6
373397.58   2061015.85  2500    8184    2496.6
374268.84   2061507.21  2580    4332    1497.9
374268.84   2061507.21  2580    4736    1507.7
374268.84   2061507.21  2580    5212    1576.2
374268.84   2061507.21  2580    5872    1713.3
374268.84   2061507.21  2580    6348    1879.8
374268.84   2061507.21  2580    6944    2095.2
374268.84   2061507.21  2580    8184    2496.6
375140.09   2061998.58  2660    4432    1499.5
375140.09   2061998.58  2660    4736    1507.7
375140.09   2061998.58  2660    5192    1576.2
375140.09   2061998.58  2660    5860    1713.3
375140.09   2061998.58  2660    6368    1870
375140.09   2061998.58  2660    6860    2046.2
375140.09   2061998.58  2660    8184    2496.6

如何使用awk开发命令?

2 个答案:

答案 0 :(得分:1)

使用此awk

awk 'NR==FNR{a[$2]=$3","$4;next} $1 in a{split(a[$1],arr,","); print arr[1],arr[2],$1,$2,$3}' file1 file2

输出:

373397.58 2061015.85 2500 4332 1497.9
373397.58 2061015.85 2500 4788 1527.3
373397.58 2061015.85 2500 5252 1586
373397.58 2061015.85 2500 5788 1703.5
373397.58 2061015.85 2500 6456 1899.4
373397.58 2061015.85 2500 7132 2124.6
373397.58 2061015.85 2500 8184 2496.6

答案 1 :(得分:0)

join -1 2 -2 1  -o 1.3 1.4 2.1 2.2 2.3 file1 file2