Merge two files into one based on the first column

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

file 1

1.00    99
2.00    343
3.00    34
...
10.00   343

file 2

1.00    0.4
2.00    0.5
3.00    0.34
...
10.00   0.9

and I want to generate the following file (using awk, bash perl):

1.00    99      0.4 
2.00    343     0.5      
3.00    34      0.34
...
10.00   343     0.9

thank

+3
source share
3 answers
join file1 file2

Files are supposed to be sorted in the merge field. If it is not, you can do this:

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

Here's the AWK version ( sortcompensates for the non-deterministic AWK order):

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

This seems shorter and more readable than Perl's answer.

In gawk4, you can set the traversal order of the array:

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

sort. @ind_num_asc - . . .

: -V (--version-sort) sort GNU sort coreutils 7.0 . @simlev, , , .

+7

Perl-

perl -anE 'push @{$h{$F[0]}}, $F[1]; END{ say "$_\t$h{$_}->[0]\t$h{$_}->[1]" for sort{$a<=>$b} keys %h }' file_1 file_2 > file_3

, awk-oneliner, , , , awk-oneliner, "pipe sort -n":

perl -anE '$h{$F[0]}="$h{$F[0]}\t$F[1]"; END{say "$_$h{$_}" for sort {$a<=>$b} keys %h}' file_1 file_2

, , .

+2

Alacon Alasql.

Node.js, Node.js, Alasql:

, :

> node alacon "SELECT * INTO TSV("main.txt") FROM TSV('data1.txt') data1 
                   JOIN TSV('data2.txt') data2 USING [0]"

. Sheet1.

0

Source: https://habr.com/ru/post/1772538/


All Articles