减去对应的行 [英] Subtract corresponding lines

查看:90
本文介绍了减去对应的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文件file1.csv

I have two files, file1.csv

3 1009
7 1012
2 1013
8 1014

和file2.csv

and file2.csv

5 1009
3 1010
1 1013

在外壳程序中,我想根据第二列中的标识符从第二个文件中的第一列中减去第二个文件中第一列中的计数.如果第二列中缺少标识符,则假定计数为0.

In the shell, I want to subtract the count in the first column in the second file from that in the first file, based on the identifier in the second column. If an identifier is missing in the second column, the count is assumed to be 0.

结果将是

-2 1009
-3 1010 
7 1012
1 1013
8 1014

文件很大(几个GB).第二列已排序.

The files are huge (several GB). The second columns are sorted.

我如何在shell中有效地做到这一点?

How would I do this efficiently in the shell?

推荐答案

假定两个文件都排在第二列:

Assuming that both files are sorted on second column:

$ join -j2 -a1 -a2 -oauto -e0 file1 file2 | awk '{print $2 - $3, $1}'
-2 1009
-3 1010
7 1012
1 1013
8 1014

join将加入排序的文件.
-j2将加入第二列.
-a1将从file1打印记录,即使file2中没有对应的行.
-a2-a1相同,但适用于file2.
在这种情况下,-oauto-o1.2,1.1,2.1相同,它将打印连接的列,然后打印文件1和文件2中的其余列. -e0将插入0而不是一个空列.这适用于-a1-a2.

join will join sorted files.
-j2 will join one second column.
-a1 will print records from file1 even it there is no corresponding row in file2.
-a2 Same as -a1 but applied for file2.
-oauto is in this case the same as -o1.2,1.1,2.1 which will print the joined column, and then the remaining columns from file1 and file2.
-e0 will insert 0 instead of an empty column. This works with -a1 and -a2.

join的输出为三列,例如:

The output from join is three columns like:

1009 3 5
1010 0 3
1012 7 0
1013 2 1
1014 8 0

通过管道传输到awk,从第2列中减去第3列,然后重新格式化.

Which is piped to awk, to subtract column three from column 2, and then reformatting.

这篇关于减去对应的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆