Awk:汇总具有相同列布局的多个文件中的列值 [英] Awk: Sum up column values across multiple files with identical column layout

查看:247
本文介绍了Awk:汇总具有相同列布局的多个文件中的列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有许多文件具有相同的标题:

I have a number of files with the same header:

COL1,COL2,COL3,COL4

COL1, COL2, COL3, COL4

您可以忽略COL1-COL3. COL4包含一个数字.每个文件包含约200行.我试图总结各行.例如:

You can ignore COL1-COL3. COL4 contains a number. Each file contains about 200 rows. I am trying to sum up across the rows. For example:

文件1

COL1 COL2 COL3 COL4
 x    y   z    3
 a    b   c    4

文件2

COL1 COL2 COL3 COL4
 x     y    z   5 
 a     b    c   10 

然后返回一个新文件:

COL1 COL2 COL3 COL4
 x     y    z   8 
 a     b    c   14

有没有一种简单的方法来执行 AWK吗?如果需要,我将使用AWK,我只是想可能会有一个简单的单行代码,我可以立即运行.我想到的AWK脚本有点长.

Is there a simple way to do this without AWK? I will use AWK if need be, I just thought there might be a simple one-liner that I could just run right away. The AWK script I have in mind feels a bit long.

谢谢

推荐答案

另一个选择.

命令:

paste f{1,2}.txt | sed '1d' | awk '{print $1,$2,$3,$4+$8}' | awk 'BEGIN{print "COL1","COL2","COL3","COL4"}1'

结果:

COL1 COL2 COL3 COL4
x y z 8
a b c 14

功能:

测试文件:

$ cat f1.txt
COL1 COL2 COL3 COL4
 x    y   z    3
 a    b   c    4

$ cat f2.txt
COL1 COL2 COL3 COL4
 x     y    z   5
 a     b    c   10

命令:paste f{1,2}.txt
联接2个文件并给出输出:

Command: paste f{1,2}.txt
Joins 2 files and gives output:

COL1 COL2 COL3 COL4 COL1 COL2 COL3 COL4
 x    y   z    3     x     y    z   5
 a    b   c    4     a     b    c   10

命令:sed '1d'
旨在暂时删除标头

Command: sed '1d'
Is meant to remove header temporarily

命令:awk '{print $1,$2,$3,$4+$8}'
返回COL1-3,并从粘贴结果中求出$ 4和$ 8.

Command: awk '{print $1,$2,$3,$4+$8}'
Returns COL1-3 and sums $4 and $8 from paste result.

命令:awk 'BEGIN{print "COL1","COL2","COL3","COL4"}1'
向后添加标题

Command: awk 'BEGIN{print "COL1","COL2","COL3","COL4"}1'
Adds header back


在@ mklement0评论之后,由于我忘记了NR==1部分,他对标头处理是正确的.


Following @mklement0 comment, he is right about header handling as I forgot the NR==1 part.

所以,我还将在这里代理他的更新版本:

So, I'll proxy his updated version here also:

paste f{1,2}.txt | awk '{ print $1, $2, $3, (NR==1 ? $4 : $4 + $8) }'

这篇关于Awk:汇总具有相同列布局的多个文件中的列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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