how to find MIN, MAX, AVG, group by date and 2 digit after comma

Viewed 22

Sample Source Data :

DATE|CPU%|MEMPHY%|MEMSWA%

2022-09-05 00|39.67|26.99|1.10

2022-09-05 00|44.94|25.42|1.10

2022-09-05 01|6.30|24.28|1.10

2022-09-05 01|4.68|26.45|1.10

2022-09-05 02|7.86|25.37|1.10

2022-09-05 02|10.66|24.38|1.10

i want to print MIN, MAX, AVR for each hour & for CPU%, MEMPHY%, MEMSWA%. the output will be like this :

  • format decimal 2 digit after comma
  • delimiter pipeline

DATE|CPU%(MIN)|CPU%(MAX)|CPU%(AVG)|MEMPHY%(MIN)|MEMPHY%(MAX)|MEMPHY%(AVG)|MEMSWA%(MIN)|MEMSWA%(MAX)|MEMSWA%(AVG)

2022-09-05 00|39.67|44.94|42.30|26.99|25.42|26.20|1.10|1.10|1.10

2022-09-05 01|6.30|4.68|5.49|24.28|26.45|25.36|1.10|1.10|1.10

2022-09-05 02|7.86|10.66|9.26|25.37|24.38|24.87|1.10|1.10|1.10

i already try below command, but :

  • cannot group by date

  • cannot format decimal 2 digit after comma.

  • cannot use same AWK for $3 & $4 at 1 line command

    cat test.txt | grep -v DATE | awk -F'|' 'NR == 1 {min = max = $2} {min = $2 < min ? $2 : min; max = $2 > max ? $2 : max; total += $2} END {print $1"|CPU%Min:",min,"|CPU%Max:",max,"|CPU%Avg:",total/NR}';

Result : 2022-09-05 02|CPU%Min: 4.68 |CPU%Max: 44.94 |CPU%Avg: 19.0183

0 Answers
Related