我有一个包含负数列表的txt文件和一个包含3列数字的csv文件。CSV文件中的所有数字都包含在txt文件中,而txt文件仅包含csv文件中存在的数字。
这是一个简短的示例:
cat list.txt
-14.5
-12.3
-10.1
-10.0
-9.9
-9.8
-7.3
-0.1
cat numbers.csv
-14.5,-14.5,-12.3
-14.5,-12.3,-10.1
-10.0,-9.9,-10.1
-7.3,-9.8,-10.1
-7.3,-7.3,-10.1
-0.1,-0.1,-9.8
我需要知道每个数字在list.txt
中的每一列中出现了多少次numbers.csv
。
在上面的示例中,预期结果应为:
-14.5: 2,1,0
-12.3: 0,1,1
-10.1: 0,0,4
-10.0: 1,0,0
-9.9: 0,1,0
-9.8: 0,1,1
-7.3: 2,1,0
-0.1: 1,1,0
是否有捷径可寻?我是bash和python的初学者。
$ awk -F, 'NR==FNR { n[NR]=$1; count=NR; next; }; '\
'{ for(i=1;i<=count;i++) { number=n[i]; '\
' if (number==$1) c[number,1]++; '\
' if (number==$2) c[number,2]++; '\
' if (number==$3) c[number,3]++; } }; '\
'END { for(i=1;i<=count;i++) { number=n[i]; text=number ": "; '\
'for(col=1;col<4;col++) { if (c[number,col]=="") c[number,col]=0; if (col>1) text=text ","; '\
'text=text c[number,col];} print text; }; }' list.txt numbers.csv
-14.5: 2,1,0
-12.3: 0,1,1
-10.1: 0,0,4
-10.0: 1,0,0
-9.9: 0,1,0
-9.8: 0,1,1
-7.3: 2,1,0
-0.1: 1,1,0
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句