How to put a difference calculation in my awk script ?

Hello,

For my CGI, I have this script :

#!/bin/bash


echo "Content-type: text/html"
echo ""

echo '
<html>
        <head>
                <meta http-equiv="Content-Type" content="test/html"; charset=UTF-8">
                <title> CLF MONITORING </title>
                <h1> FRAME monitoring <font size=3> <a href="Index.sh">[ Index ]</a> </font> </h1>
                <hr size="4" color="blue">
                
        <style>
                         body{
                          background-color: #eff1f0;
                         }
        </style>

        </head>
<body>'

read a
test=$( echo $a | cut -d'=' -f2)

echo '<PRE>'

echo "FRAME : $test "
echo "------------------"

echo ""

for fn in /var/www/cgi-bin/LPAR_MAP/*; do awk -F',|;' 'NR==1 { split(FILENAME ,a,"[-.]");print "DATE ========================== : " a[4] }
/'$test'/ { 

print ""
print "LPARS :" $2
print "RAM : " $5
print "CPU 1 : " $6
print "CPU 2 : " $7
print "" 
print ""}' $fn; done

echo '</PRE>'

echo '</body>
</html>'

This script allow to display informations from CSV like this :

FRAME : MIAIBYF00
---------------------------------


DATE ========================== : 20180122

LPARS : miaibv189
RAM : 4
CPU 1 : 0.1
CPU 2 : 0.1

LPARS : miaibp05
RAM : 61
CPU 1 : 1.1
CPU 2 : 3


LPARS : miaibp05
RAM : 61
CPU 1 : 1.1
CPU 2 : 3


DATE ========================== : 20180123

LPARS : miaibv189
RAM : 7
CPU 1 : 0.5
CPU 2 : 0.5

LPARS : miaibp05
RAM : 59
CPU 1 : 1.0 
CPU 2 : 3 


LPARS : miaibp05
RAM : 61
CPU 1 : 1.1
CPU 2 : 3

I would like to display the consumption diffenence between theses to date next to the good informations. Something like :

FRAME : MIAIBYF00
---------------------------------


DATE ========================== : 20180122

LPARS : miaibv189
RAM : 4
CPU 1 : 0.1

CPU 2 : 0.1

LPARS : miaibp05
RAM : 61
CPU 1 : 1.1
CPU 2 : 3


LPARS : miaibp05
RAM : 61
CPU 1 : 1.1
CPU 2 : 3


DATE ========================== : 20180123

LPARS : miaibv189
RAM : 7 ( + 3 )
CPU 1 : 0.5 ( + 0.4 )
CPU 2 : 0.5 ( + 0.4 )

LPARS : miaibp05
RAM : 59 ( - 2 )
CPU 1 : 1.0 ( - 0.1 )
CPU 2 : 3 


LPARS : miaibp05
RAM : 61
CPU 1 : 1.1
CPU 2 : 3
 

But I don't know how to do this... Can you help me ?

Thank yor ! :slight_smile:

...done | awk -F: '
/^DATE / {date_line++}
/^LPARS/ {lpars=$NF}
date_line==1 && $1 ~/(^RAM|^CPU)/ {var[lpars,$1]=$NF}
date_line>1 && length(var[lpars,$1]) && ($NF - var[lpars,$1] != 0) {
   if ($NF - var[lpars,$1] > 0) {
     $0=$0 "(+" $NF - var[lpars,$1] ")";
   } else {
     $0=$0 "(" $NF - var[lpars,$1] ")";
   }
}
1
'

echo '</PRE>'
.
.
.

Another solution using 1 awk

.
.
.
echo "FRAME: $test"
echo "FRAME : $test "
echo "------------------"

echo ""

awk -F',|;' -v mtch=$test '
function diff(fld,ret) {
  if (p[FNR,fld] && $fld != p[FNR,fld])
     ret= sprintf(" ( %+0.2f)", $fld - p[FNR, fld])
  p[FNR, fld] = $fld
  return $fld ret
}
FNR==1 {
  split(FILENAME ,a,"[-.]")
  print "DATE ========================== : " a[4]
}
$0 ~ mtch {
  print ""
  print "LPARS :" $2
  print "RAM : "   diff(5)
  print "CPU 1 : " diff(6)
  print "CPU 2 : " diff(7)
  print ""
  print ""
}' /var/www/cgi-bin/LPAR_MAP/*

echo '</PRE>'
.
.
.

Hello,

A late response, sorry... :wink:

Finally I do something different and make choice to create a other script for the difference calculation :

#!/bin/bash

cat /var/www/cgi-bin/test_calcul.csv | grep foo | awk -F'[,]' 'FNR==0{next}
               FNR>1{
                    print m " �' " $1;
                    print "FRAME : " $2
                    printf "RAM : %+d%s",$4-RAM,ORS
                    printf "CPU 1 : %+d%s",$5-CPU1,ORS
                    printf "CPU 2 : %+d%s\n",$6-CPU2,ORS
                    }
               {m=$1;FRAME=$2}
               {m=$1;RAM=$4}
               {m=$1;CPU1=$5}
               {m=$1;CPU2=$6}
              '

And that works !

Thanks for your help ! :slight_smile:

Allow me some nit-picking:

  • FNR never will assume the value 0 (nor does NR).
  • as you have only one input file (stdin), FNR will always be identical with NR.