Home > front end >  Grep with multiple conditions UNIX
Grep with multiple conditions UNIX

Time:03-21

I am trying to search a file and count the lines with grep and the following conditions:

  • Filter for ACCEPT
  • Date of 2018-07-04 OR Date of 2018-08-XX
  • Port number of 80 or 443

How do I go about this using grep?

I have the code as:

grep ACCEPT filename | grep -c -e '2018-07-04 \| 2018-08-[1-31]'

CodePudding user response:

You can chain grep commands to achieve an X and (Y or Z) logic, as in grep ACCEPT | grep -c -e '2018-07-04' -e '2018-08-[0-3][0-9]'.

CodePudding user response:

Suggesting to use awk script with RegExp filters pattern logic:

  awk '/ACCEPT/ && /2018-07-04|2018-08-[[:digit:]]{2}/ && /80|443/ 1' input.txt | wc -l
  • Related