比较两个文件并显示字符串重复出现的次数(Compare two files and display the count of duplicates occurences of a string)
我有两个文件:
main1.txt
111 222 333infoFile.txt
111 111 333 444我需要比较两个文件并显示文件main1.txt每行在main1.txt中重复的infoFile.txt ,例如:
111: Total 2 222: Total 0 333: Total 1我用过grep -f main1.txt infoFile.txt | sort |uniq -c grep -f main1.txt infoFile.txt | sort |uniq -c但它删除了foFile.txt中不可用的所有字符串,而我需要将它们的计数显示为0。
I have two files:
main1.txt
111 222 333infoFile.txt
111 111 333 444I need to compare both files and display how many times each line in file main1.txt is repeated in infoFile.txt, as an example:
111: Total 2 222: Total 0 333: Total 1I've used grep -f main1.txt infoFile.txt | sort |uniq -c but it removes all the strings that are not available in foFile.txt, while I need it to display the count of these as 0.
最满意答案
使用awk你可以做到:
awk 'FNR==NR{a[$1]++; next} {print $1 ": Total", ($1 in a)?a[$1]:0}' infoFile.txt main1.txt 111: Total 2 222: Total 0 333: Total 1怎么运行的:
FNR==NR - 仅对第一个文件执行此块 {a[$1]++; next} {a[$1]++; next} - 创建一个关联数组a ,键为$1 ,值为和递增计数,然后跳到下a记录 {...} - 为第二个输入文件执行此块 for (i in a)迭代数组a {print $1 ": Total", ($1 in a)?a[$1]:0} - 打印第一个字段后跟文本": Total "如果第二个文件中的第一个字段不存在于数组a则打印0。 否则从数组a打印计数。Using awk you can do:
awk 'FNR==NR{a[$1]++; next} {print $1 ": Total", ($1 in a)?a[$1]:0}' infoFile.txt main1.txt 111: Total 2 222: Total 0 333: Total 1How it works:
FNR==NR - Execute this block for first file only {a[$1]++; next} - Create an associative array a with key as $1 and value as and incrementing count and then skip to next record {...} - Execute this block for 2nd input file for (i in a) Iterate array a {print $1 ": Total", ($1 in a)?a[$1]:0} - Print first field followed by text ": Total " then print 0 if first field from 2nd file doesn't exist in array a. Otherwise print the count from array a.更多推荐
发布评论