问题描述
我使用grep在一组文件中提取行:
$ b $ grep somestring * .log
是否可以将每个文件的最大匹配数限制为每个文件的最后n个匹配项?
认为grep不支持从文件末尾限制N个匹配,所以这就是你必须做的事情。
ls * .log |同时阅读fn;做grep -iH创建$ fn|尾巴-1;完成
将 tail -1
-1替换为N.(-H选项是打印文件名,否则它将不会被打印,如果你是一个单独的文件grep,那正是我们正在做的)
注意:上面的soln可以很好地处理带有空格的文件名。
从文件开头的N个匹配
grep -i -m1 create * .log
用N代替 -m1
1。
I'm using grep to extract lines across a set of files:
grep somestring *.log
Is it possible to limit the maximum number of matches per file to the last n matches from each file?
Well I think grep does not support to limit N matches from the end of file so this is what you have to do
ls *.log | while read fn; do grep -iH create "$fn" | tail -1; done
Replace tail -1
-1 with N. (-H options is to print the file name else it wont be printed if you are grep in a single file and thats exactly we are doing above)
NOTE: Above soln will work fine with file names with spaces.
For N matches from the start of the file
grep -i -m1 create *.log
Replace -m1
1 with N.
这篇关于grep最后n匹配所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!