Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Grep lines from the log with matching pattern using Unix command

I have logs in this format. I’m looking to get logs only that has count more than 5000.

Matching pattern should be Count [value greater than 5000]

INFO 56783 Count [5987] 
INFO 67988 Count [4986] 
INFO 27679 Count [9865] 

In the above example, the output should be only

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 INFO 56783 Count [5987] 

I’m using in the following format

sudo cat <path to log> | grep 'Count [5[0-9][0-9][0-9]]'

Any ideas what is missing here

>Solution :

You may use this grep:

grep -E 'Count \[[5-9][0-9]{3,}\]' file

INFO Count [5987]
INFO Count [9865]

Here regex pattern is \[[5-9][0-9]{3,}\] that matches a:

  • \[: Match opening [
  • [5-9]: Match digits between 5 to 9
  • [0-9]{3,}: Match 3 or more digits
  • \]: Match closing ]

This will match 5000 or an integer greater than 5000 inside [...].

However, you should be using awk for this job to get most flexibility:

awk '$2 == "Count" && gsub(/[][]/, "", $3) && $3+0 > 5000' file

INFO Count 5987
INFO Count 9865
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading