Grep regex NOT containing string

前端 未结 3 357
攒了一身酷
攒了一身酷 2020-12-12 12:33

I am passing a list of regex patterns to grep to check against a syslog file. They are usually matching an IP address and log entry;

grep "1         


        
相关标签:
3条回答
  • 2020-12-12 12:42
    patterns[1]="1\.2\.3\.4.*Has exploded"
    patterns[2]="5\.6\.7\.8.*Has died"
    patterns[3]="\!9\.10\.11\.12.*Has exploded"
    
    for i in {1..3}
     do
    grep "${patterns[$i]}" logfile.log
    done
    

    should be the the same as

    egrep "(1\.2\.3\.4.*Has exploded|5\.6\.7\.8.*Has died)" logfile.log | egrep -v "9\.10\.11\.12.*Has exploded"    
    
    0 讨论(0)
  • 2020-12-12 12:45
    (?<!1\.2\.3\.4).*Has exploded
    

    You need to run this with -P to have negative lookbehind (Perl regular expression), so the command is:

    grep -P '(?<!1\.2\.3\.4).*Has exploded' test.log
    

    Try this. It uses negative lookbehind to ignore the line if it is preceeded by 1.2.3.4. Hope that helps!

    0 讨论(0)
  • 2020-12-12 12:52

    grep matches, grep -v does the inverse. If you need to "match A but not B" you usually use pipes:

    grep "${PATT}" file | grep -v "${NOTPATT}"
    
    0 讨论(0)
提交回复
热议问题