比较两个文件并显示字符串重复出现的次数(Compare two files and display the count of duplicates occurences of a string)

编程入门 行业动态 更新时间:2024-10-27 18:22:14
比较两个文件并显示字符串重复出现的次数(Compare two files and display the count of duplicates occurences of a string)

我有两个文件:

main1.txt

111 222 333

infoFile.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 333

infoFile.txt

111 111 333 444

I 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 1

I'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 1

How 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.

更多推荐

本文发布于:2023-08-02 13:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1376993.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   次数   两个   文件   Compare

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!