0

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
MLSC
  • 5,872
  • 8
  • 55
  • 89

2 Answers2

1

Don't sort the file before processing it, keep the sorting by 1st column.

Assuming you have :

file1          file 2
________________________
test2  12      test2  A
test3  17      test3  B
test4  30      test4  C
test5  5       test5  D
test6  29      test6  E

Using join file2 file1 | sort -nr -k 3 will yield :

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

use -t' ' if you want your spacing unmodified by join

Aserre
  • 4,916
  • 5
  • 33
  • 56
1

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
konsolebox
  • 72,135
  • 12
  • 99
  • 105