0

How to search for 2 patterns and limit them at the same time?

example: I have 50 lines, 25 has "kernel" in it, another 25 has "audit". How can I filter out the output so i get 5 lines of each with a single command? Is it possible with grep?

6
  • you mean 5 lines have both kernel and audit in it? – pLumo Apr 20 '20 at 12:28
  • nah I meant I need 10 lines, 5 with kernel and 5 with audit separately. – Kendzio Apr 20 '20 at 12:30
  • just the first 5 matches for each? – pLumo Apr 20 '20 at 12:31
  • I think this does the trick, using awk: Need to grep for first occurrences of multiple strings – Krackout Apr 20 '20 at 12:34
  • Does kernel and audit appear on the same line? If yes, what to do with it? print it for each audit and kernel or just once? – pLumo Apr 20 '20 at 12:38
1

What about awk ?

awk '
     k<5 && /\<kernel\>/ {k++;print}
     a<5 && /\<audit\>/ {a++;print}
' file

Or 2 grep commands:

grep -m 5 -Fw kernel file
grep -m 5 -Fw audit file

Leave out the word boundaries from awk and the -w from grep if you don't want this.

1
  • It worked! thank you! – Kendzio Apr 20 '20 at 14:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.