使用grep在大量日志文件中计算标签的所有出现次数。

huangapple go评论60阅读模式
英文:

Count all occurrences of a tag in lots of log files using grep

问题

我需要获取标签数量,例如“103=16”,在许多文件中找到了多少个,但只有那些至少出现一次的文件。

我正在使用:

find . /opt/FIXLOGS/l51prdsrv* -iname "TRADX_*oe*.log" -type f -exec grep -F 103=16 -c {} /dev/null ;

这会找到包含标签的文件并显示匹配的数量,但也会显示出现0次的文件。

返回结果如下:

file1.log:0
file2.log:0
file3.log:6
file4.log:0

使用-i排除0或使用grep -v :0对我没有起作用,会得到以下结果:

grep: :0: No such file or directory

我该如何只获取匹配次数大于0的文件呢?

英文:

I need to get the quantity of tags, for example "103=16" found in lots of files, how many of them are, but only the files that have one or more occurrences

I'm using:

find . /opt/FIXLOGS/l51prdsrv\* -iname "TRADX\_*oe*.log" -type f -exec grep -F 103=16 -c {} /dev/null ;

which finds the file where the tag is and shows the number of matches, but it also shows the 0 occurrences

returns

file1.log:0

file2.log:0

file3.log:6

file4.log:0

using a -i to exclude the 0 or grep -v :0 haven't worked for me, gets the result:

grep: :0: No such file or directory

How can I get only the files where the count is more than 0?

答案1

得分: 3

尝试使用管道符号(|)连接到grep命令,以排除在find/exec后结果中包含零的部分。

例如,以下命令在我的环境中有效:

find . -type f -iname "TRADX_oe.log" -exec grep -cFH "103=16" {} \; | grep -v ":0"
英文:

Have you tried piping into grep to negate the ones with zeroes after the find/exec?

E.g., like this works for me:

find . -type f -iname "TRADX_oe.log" -exec grep -cFH "103=16" {} \; | grep -v ":0"

答案2

得分: 1

Using awk to do everything in one place

find . -type f -iname "TRADX_oe.log" -exec awk '/103=16/{c++} END { if(c)print FILENAME, c}' {} \;
英文:

Using awk to do everything in one place

find . -type f -iname "TRADX_oe.log" -exec awk '/103=16/{c++} END { if(c)print FILENAME, c}' {} \;

答案3

得分: 0

这是grep命令中-c选项的工作方式:

> -c, --count
> 抑制正常输出;而是为每个输入文件打印匹配行的计数。 使用-v、--invert-match选项(参见下文),可以计算非匹配行数。

因此,它将打印0计数,唯一的选择是使用-v选项删除0或使用awk:

awk '/search_pattern/{f[FILENAME]+=1} END {for(i in f){print i":"f[i]}}' /path/to/files*
英文:

That is the way how -c option of grep works:

> -c, --count
> Suppress normal output; instead print a count of matching lines for each input file. With the -v, --invert-match
> option (see below), count non-matching lines.

So it will print 0 counts, only option is to remove 0 with another grep using -v or use awk:

awk '/search_pattern/{f[FILENAME]+=1} END {for(i in f){print i":"f[i]}}' /path/to/files*

答案4

得分: 0

它在我将grep管道到分号后面(不包括零)时起作用 | grep -v ":0"

以这种方式结束:

find . route -iname "TRAD_oe.log" -type f -exec grep -cHF "103=16" {} ; | grep -v ":0"

英文:

It worked when i pipe the grep after the ; excluding the zero | grep -v ":0"

ending like this:

find . route -iname "TRAD_oe.log" -type f -exec grep -cHF "103=16" {} ; | grep -v ":0"

huangapple
  • 本文由 发表于 2023年2月8日 15:06:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75382382.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定