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
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.