Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search for multiple strings in a log file using less command in unix?

I want to search for multiple strings in a log file. Only those entries should be highligted where all the search strings are there in same line. Can i use less command for this or any other better option. My log file size is typically few GBs.

like image 571
Satish Avatar asked Jan 27 '17 19:01

Satish


2 Answers

When you want to search for string1 or string2, use /string1|string2. You said you wanted lines where you find both:

/string1.*string2

When you do not know the order in the line and want to see the complete line, you will need

/.*string1.*string2.*|.*string2.*string1.*

Or shorter

/.*(string1.*string2|string2.*string1).*

Combining more words without a fixed order will become a mess, and filtering first with awk is nice.

like image 98
Walter A Avatar answered Sep 17 '22 14:09

Walter A


Use awk to filter the file and less to view the filtered result:

awk '/pattern1/ && /pattern2/ && /pattern3/' file.log | less

If the file is big you may want to use stdbuf to see results earlier in less:

stdbuf awk '/pattern1/ && /pattern2/ && /pattern3/' file.log | less
like image 23
hek2mgl Avatar answered Sep 19 '22 14:09

hek2mgl