Search code examples
bashjoinawksedcut

Merging two files with columns in bash


I have a file1.txt and the output is:

test4  30
test6  29
test3  17
test2  12
test5  5

This is file is ordered by second column. I sorted it with sort -nr -k 2 .

I have also file2.txt with the content of:

test2   A
test3   B
test4   C
test5   D
test6   E

What I want as result(result.txt) is:

test4   C  30
test6   E  29
test3   B  17
test2   A  12
test5   D  5

Solution

  • Using awk:

    awk 'FNR == NR { a[$1] = $2; next } { print $1,  a[$1], $2 }' file2 file1
    

    Output:

    test4 C 30
    test6 E 29
    test3 B 17
    test2 A 12
    test5 D 5
    

    If file1 is not yet sorted, you can do:

    sort -nr -k 2 file1 | awk 'FNR == NR { a[$1] = $2; next } { print $1, a[$1], $2 }' file2 -
    

    Or

    awk 'FNR == NR { a[$1] = $2; next } { print $1,  a[$1], $2 }' file2 <(sort -nr -k 2 file1)
    

    There are many ways to format the output. You can use column -t:

    ... | column -t
    

    Output:

    test4  C  30
    test6  E  29
    test3  B  17
    test2  A  12
    test5  D  5
    

    Or you can use printf. Although I'd prefer using column -t since table would be broken if one column grows larger than the actual size that printf has provided.

    ... { printf "%s%3s%4.2s\n", $1, a[$1], $2 }' ...
    

    Output:

    test4  C  30
    test6  E  29
    test3  B  17
    test2  A  12
    test5  D   5