I'm trying to determine the maximum values of columns S2 & S4 but only for the corresponding S1 columns that have BBB.
The output would be S2 max=800 and S4 max=90.
Currently have:
awk 'BEGIN { FS = " " ; OFS = " "; } { if(NR%3==0){max=$3} { s2+=substr($2,1,3); s4+=substr($4,1,2) } } ; { $3 >= max } END { print "Max S2 = " s2, "; Max S4 = " s4 } ' file.txt
S1 S2 S3 S4
--- --- - --
AAA 100 A 99
BBB 200 B 90
CCC 300 C 80
AAA 400 A 70
BBB 500 B 60
CCC 600 C 50
AAA 700 A 40
BBB 800 B 30
CCC 900 C 20
AAA 999 A 10
The shell you are using should not make any difference, it doesn't change the way awk
behaves. It may change the way you provide variable input to awk
, but your problem here doesn't require any.
Here you can simply identify the records (lines) containing BBB
and then keep track of the maximum values for field-2 and field-4 and then output the results using the END
rule. You can use a traditional if
or a ternary operator as @RavinderSingh13 has used. A traditional if
would be:
awk '/BBB/{
if ($2 > s2)
s2 = $2
if ($4 > s4)
s4 = $4
}
END{
printf "S2 max=%d and S4 max=%d\n", s2, s4
}' file
Example Use/Output
With your data in the filename file
, you can just change to the directory containing file
(make sure you change file
to whatever your input file name is) and then select-copy above and middle-mouse-paste into the terminal to test, e.g.
$ awk '/BBB/{
> if ($2 > s2)
> s2 = $2
> if ($4 > s4)
> s4 = $4
> }
> END{
> printf "S2 max=%d and S4 max=%d\n", s2, s4
> }' file
S2 max=800 and S4 max=90