Home > Enterprise >  Get top 10 values from log file using shell script
Get top 10 values from log file using shell script

Time:02-20

I have to create a report by reading a huge log file.

The log file contains data something like this.

REQUEST Call set up for X5356E08XWZV0ZVAE1 1645167707895 1645167709644 OK
REQUEST Call to database : X5356E08XWZV0ZVAE2 1645167709651 1645167709748 OK
REQUEST Call set up for X5356E08XWZV0ZVAE3 1645167709750 1645167709874 OK
REQUEST Set Mau Preparation for X5356E08XWZV0ZVAE4 1645167709875 1645167709991 OK
REQUEST Call track key end point for X5356E08XWZV0ZVAE5 1645167709992 1645167710379 KO

Now, I need to write a shell script. If First column is REQUEST, then subtract NF-2 from NF-1 and print top 3 USERS. If the line contains KO, ignore that line.

I am trying to build a simple logic awk but not much success. Any help greatly appreciated. I tried below code but it just sorts the time but I need to print user name too if the first column is REQUEST.

awk '$1 == "REQUEST" {print $(NF-3)" "  $(NF-1) - $(NF-2)}' "path/simulation_1.log"

I got response USER1 1000 USER2 2000 . . I want the reponse to be sorted descending and only 10 entries.

CodePudding user response:

awk 'BEGIN {printf "Top ten User are\n"} {if ($0 !~ "KO" && $1 == "REQUEST") {print $2" time taken "$3-$4" seconds" | "sort -nrk4 | head -n10 "}}'
  • Related