Search code examples
grepfindack

how to exclude some of the matches from grep?


I am using grep to printout the matching lines from a very large file from which i got hundreds of matches, some of them are not interesting i want to exclude those matching which are not interesting

grep "WARNING" | grep -v "WARNING_HANDLING_THREAD" path # i tried this

When I grep the file for warning I get

0-00:00:33.392 (2127:127:250:02 = 21.278532 Fri Feb 1 10:17:22 2019) <3:0x000a>:[89]:[enter]: cest_handleFreeReq.c:116: [WARNING]: cest_handleFreeReq: sent from DECA ->UCS

0-00:00:38.263 (2189:022:166:06 = 21.891510 Fri Feb 1 10:17:28 2019) <3:0x000a>:[89]:[enter]: cest_handleConfigReq.c:176: [WARNING]: cest_handleConfigReq.c: GroupConfig NOT present.

0-00:00:38.263 (2189:022:167:03 = 21.891510 Fri Feb 1 10:17:28 2019) <3:0x000a>:[89]:[enter]: cest_handleConfigReq.c:194: [WARNING]: cest_handleConfigReq: physicalConfig NOT present.

60 0x6d77 0 0x504ea | 2 18 | 0 0 | 4 12 | 647 | 14685 0 0.0 0 500 500 | 0 | 0 | 38 | ETH_DRV_WARNING_HANDLING_thread 60 0 | 0 0 | 0 0 0 | 0 0 0 0 0 0 ! N/A N/A N/A N/A N/A N/A |ETH_DRV_WARNING_HANDLING_thread

WARNING: List of threads violating the heap & stack limit

I want to exclude the last lines which are not interesting

0-00:00:33.392 (2127:127:250:02 = 21.278532 Fri Feb 1 10:17:22 2019) <3:0x000a>:[89]:[enter]: cest_handleFreeReq.c:116: [WARNING]: cest_handleFreeReq: sent from DECA ->UCS

0-00:00:38.263 (2189:022:166:06 = 21.891510 Fri Feb 1 10:17:28 2019) <3:0x000a>:[89]:[enter]: cest_handleConfigReq.c:176: [WARNING]: cest_handleConfigReq.c: GroupConfig NOT present.

0-00:00:38.263 (2189:022:167:03 = 21.891510 Fri Feb 1 10:17:28 2019) <3:0x000a>:[89]:[enter]: cest_handleConfigReq.c:194: [WARNING]: cest_handleConfigReq: physicalConfig NOT present.

Is there a way to do this using grep find or any other tool?

Thank you


Solution

  • Note that the substring thread is in lower case in the data, but in upper case in your expression.

    Instead, use

    grep -F 'WARNING' logfile | grep -F -v 'WARNING_HANDLING_thread'
    

    The -F make grep use string comparisons rather than regular expression matching (this is not really related to your current issue, but just a way of showing that we know what type of pattern we're matching with).

    Another option would be to make the second grep do case insensitive matching with -i:

    grep -F 'WARNING' logfile | grep -Fi -v 'WARNING_HANDLING_THREAD'
    

    In this case though, I would probably match the [WARNING] tag instead:

    grep -F '[WARNING]:' logfile
    

    Note that here we need the -F so that grep interprets the pattern as a string and not as a regular expression matching any single character out of the W, A, R, N, I, G set, followed by a :.