大佬教程收集整理的这篇文章主要介绍了如何用awk对重复行的值求和?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
Order Date,Username,Order number,No Resi,Quantity,title,update Date,Status,Price Per Item,Status Tracking,Alamat 05 Jun 2018,Mildred@email.com,205583995140400,2,Gold,05 Jun 2018 – 10:01,In Process,Rp3.000.000,Done,Syahrul Address 05 Jun 2018,1,Martha@email.com,205486016644400,Faishal Address 05 Jun 2018,Misty@email.com,205588935534900,Rutwan Address 05 Jun 2018,Rutwan Address
我想删除该文件中的重复项并将数量行中的值相加.我希望结果是这样的:
Order Date,3,4,Rutwan Address
我只想将数量行中的值相加,而将其余部分保留原样.我在this question尝试过这个解决方案,但只有文件只有2行才能解决问题,我有11个,所以它不起作用.我怎么用awk做到这一点?
awk -F,' FNR==1{ print; next} { val=$5; $5="~"; a[$0]+=val } !b[$0]++{ c[++count]=$0} END{ for(i=1;i<=count;i++){ sub("~",a[c[i]],c[i]); print c[i]} }' OFS=,Input_file
说明:现在也向上面的代码添加说明.
awk -F,' ##SetTing field separator as comma here. FNR==1{ ##checking condition if line number is 1 then do following. print; ##Print the current line. next} ##next will skip all further statements from here. { val=$5; ##CreaTing a variable named val whose value is 5th field of current line. $5="~"; ##SetTing value of 5th field as ~ here to keep all lines same(to create index for array a). a[$0]+=val ##CreaTing an array named a whose index is current line and its value is variable val value. } !b[$0]++{ ##checking if array b whose index is current line its value is NULL then do following. c[++count]=$0} ##CreaTing an array named c whose index is variable count increasing value with 1 and value is current line. END{ ##StarTing END block of awk code here. for(i=1;i<=count;i++){ ##StarTing a for loop whose value starts from 1 to till value of count variable. sub("~",c[i]); ##SubstituTing ~ in value of array c(which is actually lines value) with value of SUMMED $5. print c[i]} ##PrinTing newly value of array c where $5 is Now replaced with its actual value. }' OFS=,Input_file ##SetTing OFS as comma here and mentioning Input_file name here too.
以上是大佬教程为你收集整理的如何用awk对重复行的值求和?全部内容,希望文章能够帮你解决如何用awk对重复行的值求和?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。