Home > database >  Grep lines only if containing 2 specific strings
Grep lines only if containing 2 specific strings

Time:12-17

Is it possible to grep lines only if containing 2 specific strings in it?

I have logs like this and I would like to grep only lines containing strings "13/Dec/2022:11" AND "GET /fr/test"

access.log:site.ch 111.222.333.444 - - [13/Dec/2022:11:40:00  0100] "GET /fr/test HTTP/1.1" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"

CodePudding user response:

Like this, just one awk:

awk '/13\/Dec\/2022:11/ && /GET \/fr\/test/' file

or one perl:

perl -ne 'print if m|13/Dec/2022:11| and m|GET /fr/test|' file

or one sed:

sed -n '/13\/Dec\/2022:11/,/GET \/fr\/test/p' file

or with one zgrep or grep:

zgrep -E '13/Dec/2022:11.*?GET /fr/test' file

CodePudding user response:

Just grep twice:

| grep "13/Dec/2022:11" | grep "GET /fr/test"

CodePudding user response:

With GNU grep and 2 strings:

grep -E 'string1.*string2|string2.*string1' file
  • Related