Merging two files into one based on the first column

后端 未结 3 924
隐瞒了意图╮
隐瞒了意图╮ 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:34

    A Perl-solution

    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
    

    Ok, looking at the awk-oneliner this is shorter then my first try and it has the nicer output then the awk-oneliner and it doesn't use the '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
    

    And the one-liners behave different then the join-example if there are entries with no value in the second column in the first file.

提交回复
热议问题