简体   繁体   English

Bash - 计算一行出现的频率

[英]Bash - Count how often a line appears

File A is the file I have to search through.文件 A 是我必须搜索的文件。 File B is the File, that has what I need to search in file A.文件 B 是文件,其中包含我需要在文件 A 中搜索的内容。

File A is a lot of code, simular to this:文件 A 是很多代码,类似这样:

test test 
a = rr
b = gg
test test
c = a + b
test 

Those variables are sometimes only declared, and not used, and I filtered all variables, that I need out, into File B:这些变量有时只声明而不使用,我将所有需要的变量过滤到文件 B 中:

a
b

I want to count how often each variable (each variable in File B has its own line, and is a string), appears in File B.我想计算每个变量(文件 B 中的每个变量都有自己的行,并且是一个字符串)在文件 B 中出现的频率。

I would use count if in Excel to get this going, but I don´t know how to do something simular in Bash.我会在Excel中使用count if来实现这一点,但我不知道如何在 Bash 中做一些类似的事情。

I tried to use grep -wc File_B File_A But this did not work for me, I guess its because this try´s to find the complete File_B in File_A, but that does not work.我尝试使用grep -wc File_B File_A但这对我不起作用,我猜是因为这个尝试在 File_A 中找到完整的 File_B,但这不起作用。

Any help would be apprechiated.任何帮助将不胜感激。

Kind regards亲切的问候

Elias埃利亚斯

The command grep -wc File_B File_A actually searches for the word File_B in the file File_A .命令grep -wc File_B File_A实际上在文件File_A中搜索单词File_B

What you may have wanted is grep -wcf File_B File_A - the -f means "don't search for the pattern File_B , instead read the patterns to search from File_B .您可能想要的是grep -wcf File_B File_A - -f的意思是“不要搜索模式File_B ,而是读取要从File_B搜索的模式。

The thing is, that will count all the matches from File_B that are found in File_A and sum it for you, so if the pattern on the first line of File_B is found 8 times, and the pattern on the second line of File_B is found 4 times, grep -wcf will just print 12. If you want to have an output that lists how many times each pattern was found, you'd want to write a loop that reads each line from File_B and grep s for it individually.问题是,它将计算在 File_A 中找到的来自File_B的所有匹配File_A并为您求和,所以如果File_B第一行上的模式被找到 8 次,并且File_B第二行上的模式被找到 4时间, grep -wcf只会打印 12。如果您想要一个输出列出每个模式被找到的次数,您需要编写一个循环,从File_Bgrep中分别读取每一行。

Maybe something like this:也许是这样的:

while read pat; do echo -n "$pat: "; grep -wc "$pat" File_A; done < File_B

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM