-1

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?

1
  • 1
    replace val=$1/$2 with val=p/$2 Commented May 5, 2017 at 14:36

1 Answer 1

1

From first description

 awk -F, '$1<prev{print prev/$2;exit}{prev=$1}' <input.txt

At the end of each line, 1st column is stored in prev Then when value of 1st column is least than prev, it prints the ratio and exits

Sign up to request clarification or add additional context in comments.

7 Comments

I have already done that
can you explain "loop through the values in two columns and perform control statements" with examples input and expected output
The example is in the first paragraph, dear.
Exactly where in the first paragraph? I don't understand that question either.
So the desired output of your script given your posted sample input is 0.68? Whats all that stuff about the criteria for my if-else statement will be something like (if the value is >=0.8 and <=0.9, print A, else if the value) is >=0.7 and <=0.8, print B, if the value is >=0.5 and <=0.7 print C otherwise print D). related to? Your question is not clear at all. Please edit your question to clearly state the expected output given that input and explain what all that other logic you describe is about.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.