How to search multiple lines in a log file using a smaller command in unix?

I want to find some lines in a log file. Only those entries should be high-ranking, where all search strings are on the same line. Can I use a smaller command for this or any other better option. My log file size is usually a few GB.

+5
source share
2 answers

If you want to find string1 or string2 , use /string1|string2 . You said you want lines where you find both:

 /string1.*string2 

If you do not know the order in the line and want to see the full 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 with awk will be enjoyable.

+6
source

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

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

If the file is large, you can use stdbuf to view the results earlier in less :

 stdbuf awk '/pattern1/ && /pattern2/ && /pattern3/' file.log | less 
+3
source

Source: https://habr.com/ru/post/1263466/


All Articles