I am working with a huge CSV file (filtest.csv) that contains two columns. From column 1, I wanted to read current row and compare it with the value of the previous row. If it is greater OR equal, continue comparing and if the value of the current cell is smaller than the previous row - then i wanted to jump to the second column and take the value in the current row (of the second column). Next I wanted to divided the larger value we got in column 1 by the value in the same cell of column two with that of the smaller value in column 1. Let me clarify with this example. For example in the following table: the smaller value we will be depending on my requirement from Column 1 is 327 (because 327 is smaller than the previous value 340) - and then we take 500 (which is the corresponding cell value on column 2). Finally we divide 340 by 500 and get the value 0.68. My bash script should exit right after we print the value to the console.
338,800
338,550
339,670
340,600
327,500
301,430
299,350
284,339
284,338
283,335
283,330
283,310
282,310
282,300
282,300
283,290
In the following script, I tried it to do the division operation on the same row of the two columns and it works fine
awk -F, '$1<p && $2!=0{
val=$1/$2
if(val>=0.85 && val<=0.9)
{
print "value is:" $1/p
print "A"
}
else if(val==0.8)
{
print "B"
}
else if(val>=0.5 && val <=0.7)
{
print "C"
}
else if(val==0.5)
{
print "E"
}
else
{
print "D"
}
exit
}
{
p=$1
}' filetest.csv
But how can we loop through the values in two columns and perform control statements on two different rows of the two columns as i mentioned earlier?
val=$1/$2withval=p/$2