Search code examples
linuxfileawkcommandecho

Linux command shell script


I am searching in linux logs file which have some error code I wants to calculate these code on hourly basis but my requirement is that response code should be show in below form . In attach file data is not fullfill my requirement can anybody help on this . My current command

for sample logs -:

IP web.com - [01/Aug/2023:01:27:21 +0200] "POST /aa/v1/bbb/session?device_id=urn:uuid:2972033-34a4-4d1c-8ctt-55d31140c09d HTTP/1.1" 503 0 "-" "IOS/2.1.0 (Linux; U; Android 8.1.0; ttw Build/OTT 1.171019.011)"

code -:

for i in {00..23} ; do  grep "pattern" access.log |grep "03/Aug/2023:$i" |awk -F " " '{print $4,$9}' |awk '{print $NF}'|sort |uniq -c ; done
 91 200
  1 429
 46 200
  1 503
 11 200
  4 200
  7 200
  9 200
 37 200
  1 403

in this output getting response code counts, but i wants one more column which have hours before this above data . for example

  hrs   Counts  Response code 
 00    91 200
 00    1 429
 01    46 200
 01    1 503
 02   11 200

Hrs counts error code in respective column[text](this is my current output but it is not working for me I wants the data as I shown example above )


Solution

  • awk -v pattern="some pattern" -v day='01/Aug/2023' '
        $0 ~ pattern && $4 ~ day {
            split($4,a,":")
            codes[a[2]" "$9]++
        }
        END{
            for (i in codes){
                split(i,b)
                print b[1], codes[i], b[2]
            }
        }
    ' access.log
    
    01 4 503
    02 1 200
    02 1 404
    02 2 503
    03 1 200