regexp match within a log file, return dynamic content above and below match

后端 未结 4 1728
Happy的楠姐
Happy的楠姐 2020-12-12 03:06

I have some catchall log files in a format as follows:

timestamp event summary
foo details
account name: userA
bar more details
timestamp event summary
baz d         


        
4条回答
  •  [愿得一人]
    2020-12-12 03:51

    This is all you need with GNU awk (for IGNORECASE):

    $ cat tst.awk
    function prtRecord() {
        if (record ~ regexp) {
            printf "-------- start of record %d --------%s", ++numRecords, ORS
            printf "%s", record
            printf "--------- end of record %d ---------%s%s", numRecords, ORS, ORS
        }
        record = ""
    }
    BEGIN{ IGNORECASE=1 }
    /^[[:digit:]]+-[[:digit:]]+-[[:digit:]]+/ { prtRecord() }
    { record = record $0 ORS }
    END { prtRecord() }
    

    or with any awk:

    $ cat tst.awk
    function prtRecord() {
        if (tolower(record) ~ tolower(regexp)) {
            printf "-------- start of record %d --------%s", ++numRecords, ORS
            printf "%s", record
            printf "--------- end of record %d ---------%s%s", numRecords, ORS, ORS
        }
        record = ""
    }
    /^[[:digit:]]+-[[:digit:]]+-[[:digit:]]+/ { prtRecord() }
    { record = record $0 ORS }
    END { prtRecord() }
    

    Either way you'd run it on UNIX as:

    $ awk -v regexp=user6q -f tst.awk file
    

    I don't know the Windows syntax but I expect it's very similar if not identical.

    Note the use of tolower() in the script to make both sides of the comparison lower case so the match is case-insensitive. If you can instead pass in a search regexp that's the correct case, then you don't need to call tolower() on either side of the comparison. nbd, it might just speed the script up slightly.

    $ awk -v regexp=user6q -f tst.awk file
    -------- start of record 1 --------
    2013-03-25 08:02:32 Auth.Critical   169.254.8.110   Mar 25 08:02:32 dc3 MSWinEventLog   2   Security
        11730159    Mon Mar 25 08:02:29 2013    680 Security    NT AUTHORITY\SYSTEM N/A Audit Failure
    dc3 9   Logon attempt by:   MICROSOFT_AUTHENTICATION_PACKAGE_V1_0
    
    Logon account:  USER6Q
    
    Source Workstation: dc3
    
    Error Code: 0xC0000234
    --------- end of record 1 ---------
    
    -------- start of record 2 --------
    2013-03-25 08:02:32 Auth.Critical   169.254.8.110   Mar 25 08:02:32 dc3 MSWinEventLog   2   Security
        11730160    Mon Mar 25 08:02:29 2013    539 Security    NT AUTHORITY\SYSTEM N/A Audit Failure
    dc3 2   Logon Failure:
    
        Reason:     Account locked out
    
        User Name:  USER6Q@MYDOMAIN.TLD
    
        Domain: MYDOMAIN
    
        Logon Type: 3
    
        Logon Process:  Advapi
    
        Authentication Package: Negotiate
    
        Workstation Name:   dc3
    
        Caller User Name:   dc3$
    
        Caller Domain:  MYDOMAIN
    
        Caller Logon ID:    (0x0,0x3E7)
    
        Caller Process ID: 400
    
        Transited Services: -
    
        Source Network Address: 169.254.7.89
    
        Source Port:    55314
    --------- end of record 2 ---------
    

提交回复
热议问题