在bash中合并两个带有列的文件

时间:2014-07-24 12:44:28

标签: bash join awk sed cut

我有一个file1.txt,输出是:

test4  30
test6  29
test3  17
test2  12
test5  5

此文件由second column订购。我用sort -nr -k 2对其进行了排序。

我还有file2.txt,内容为:

test2   A
test3   B
test4   C
test5   D
test6   E

我想要的结果(result.txt)是:

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

2 个答案:

答案 0 :(得分:1)

在处理文件之前不要对文件进行排序,请按第1列进行排序。

假设你有:

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

使用join file2 file1 | sort -nr -k 3将产生:

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

如果您希望通过加入

修改间距,请使用-t' '

答案 1 :(得分:1)

使用awk

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

输出:

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

如果file1尚未排序,您可以执行以下操作:

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

或者

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

格式化输出的方法有很多种。您可以使用column -t

... | column -t

输出:

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

或者您可以使用printf。虽然我更喜欢使用column -t,因为如果一个列的长度大于printf提供的实际大小,则表格会被破坏。

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

输出:

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