Merging two files into one based on the first column

后端 未结 3 927
隐瞒了意图╮
隐瞒了意图╮ 2021-01-14 11:13

I have two files, both in the same format -- two columns both containing a number, for example:

file 1

1.00    99
2.00    343
3.00           


        
3条回答
  •  余生分开走
    2021-01-14 11:28

    join file1 file2
    

    Which assumes that the files are sorted on the join field. If they are not, you can do this:

    join <(sort -V file1) <(sort -V file2)
    

    Here's an AWK version (the sort compensates for AWK's non-deterministic array ordering):

    awk '{a[$1]=a[$1] FS $2} END {for (i in a) print i a[i]}' file1 file2 | sort -V
    

    It seems shorter and more readable than the Perl answer.

    In gawk 4, you can set the array traversal order:

    awk 'BEGIN {PROCINFO["sorted_in"] = "@ind_num_asc"} {a[$1]=a[$1] FS $2} END {for (i in a) print i a[i]}' file1 file2
    

    and you won't have to use the sort utility. @ind_num_asc is Index Numeric Ascending. See Controlling Array Traversal and Array Sorting and Using Predefined Array Scanning Orders with gawk.

    Note that -V (--version-sort) in the sort commands above requires GNU sort from coreutils 7.0 or later. Thanks for @simlev pointing out that it should be used if available.

提交回复
热议问题