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?
The grep utility searches the input files, selecting lines matching one or more patterns; the types of patterns are controlled by the options specified. The patterns are specified by the -e option, -f option, or the pattern_list operand.
In Linux and Unix Systems Grep, short for “global regular expression print”, is a command used in searching and matching text files contained in the regular expressions.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With