Home > Software engineering >  If an awk IF is met, do another awk
If an awk IF is met, do another awk

Time:09-23

Very new to awk and bash. I am trying to complete an analysis on a log file. The file contains a time value, that I would like give a min and max value for, but I need to complete this only for a specific url string that is found in the log.

Below is an example of the data:

[rootawk]# cat testdata.txt
123 192.168.1.1 edge 20 /url/test/1
123 192.168.1.1 edge 10 /url/test/1
123 192.168.1.1 edge 30 /url/test/1
123 192.168.1.1 google 70 /url/test/3
124 192.168.1.1 edge 25 /url/test/3
124 192.168.1.1 google 15 /url/test/3

I can run and awk command of the testdata.txt that only selects certain criteria:

awk '{if ($1 == 123 && $5 == "/url/test/1") {print $1,$2,$3,$4,$5 }}' testdata.txt

I can also run a Min Max awk to give me the min Max of the testdata.txt (part2)

awk '(NR==1){Min=$4;Max=$4};(NR>=2){if(Min>$4) Min=$4;if(Max<$4) Max=$4} END {printf "The Min is %d ,Max is %d",Min,Max}' testdata.txt

However, I would like to be able to do is run the MinMax (part2) on the output of the search criteria (Part1).

So The response would be in this case:

Number of /url/test/1 = 3 with a Time; Min: 10 Max:30

I am not sure how best to approach this.

I found this link, but it is not clear how I run this against a logfile. (this will be set to $1 in bash script)

Execute another awk from awk file

TIA

CodePudding user response:

Your code:

awk '{if ($1 == 123 && $5 == "/url/test/1") {print $1,$2,$3,$4,$5 }}' testdata.txt

is more simply written:

awk '$1==123 && $5=="/usr/test/1" {print}' testdata.txt

or even just:

awk '$1==123 && $5=="/usr/test/1"' testdata.txt

You can use the negation of that to skip records you don't care about, then do the min/max tests:

awk '
    !( $1==123 && $5=="/usr/test/1" ) { next }
    !n   { Min=Max=$4; next }
    Min>$4 { Min=$4 }
    Max<$4 { Max=$4 }
    END { printf "Number of /usr/test/1 = %d with a Time; Min: %d Max:%d\n", n,Min,Max }
' testdata.txt

CodePudding user response:

Initialize Min and Max to the empty string, then update Min and Max on lines that match the filter criteria. In your END block, exit early if Min is still the empty string it was initialized to. (Either both variables are the empty string or neither one is.)

$ cat script.awk
$1 == 123 && $5 == "/usr/test/1" { 
  if (Min == "" || Min > $4) { Min = $4 }
  if (Max == "" || Max < $4) { Max = $4 }
}
END {
  if (Min == "") { exit; }
  printf "The Min is %d ,Max is %d\n",Min,Max}
}
$ awk -f script.awk testdata.txt
  • Related