英文:
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"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论