[英]AWK command for sum 2 files
i am new at awk and i need awk command to summing 2 files if found the same column 我是awk的新手,如果发现同一列,我需要awk命令来汇总2个文件
file 1 文件1
a | 16:00 | 24
b | 16:00 | 12
c | 16:00 | 32
file 2 文件2
b | 16:00 | 10
c | 16:00 | 5
d | 16:00 | 14
and the output should be 并且输出应该是
a | 16:00 | 24
b | 16:00 | 22
c | 16:00 | 37
d | 16:00 | 14
i have read some of the question here and still found the correct way to do it, i already tried with this command 我在这里阅读了一些问题,但仍然找到正确的方法,我已经尝试过使用此命令
awk 'BEGIN { FS = "," } ; FNR=NR{a[$1]=$2 FS $3;next}{print $0,a[$1]}'
please help me, thank you 请帮助我,谢谢
This script also uses sort
but it will work, 这个指令码也使用了sort
但可以运作
awk -F'|' ' { f[$1] += $3 ; g[$1] = $2 } END { for (a in f) { print a , "|", g[a] , "|", f[a] } } ' a.txt b.txt | sort
The results are 结果是
a | 16:00 | 24
b | 16:00 | 22
c | 16:00 | 37
d | 16:00 | 14
无|sort
awk -F'|' '{O[$1FS$2]+=$3}END{asorti(O,T,"@ind_str_asc");for(t in T)print T[t] FS O[T[t]]}' file[1,2]
Just store all the data in two arrays a[]
and b[]
and then print them back: 只需将所有数据存储在两个数组a[]
和b[]
,然后将它们打印回来:
awk 'BEGIN{FS=OFS="|"}
{a[$1]+=$3; b[$1]=$2}
END{for (i in a) print i,b[i],a[i]}' f1 f2
$ awk 'BEGIN{FS=OFS="|"} {a[$1]+=$3; b[$1]=$2} END{for (i in a) print i,b[i],a[i]}' f1 f2
b | 16:00 |22
c | 16:00 |37
d | 16:00 |14
a | 16:00 |24
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.