1

I am working with a huge CSV file (filename.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 value larger value we got in column 1 by the value in the same cell of column two. For example in the following table: the smaller value we will get depending on my requirement is 327 (because 327 is smaller than the previous value 340) - and then we take 500 (which is the corresponding cell value on column two). 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, it works only for column 1 but i want to extend it for two columns based on my criteria i mentioned above.

awk '$1<p{ 
    val=$1/p                   
    if(val>=0.8 && val<=0.9)
        {
            print "value is:" $1/p
            print "A"
        }
    else if(val==0.8)
        {
            print "B"
        }
    else if(val>=0.6 && val <=0.7)
        {
            print "C" 

        }
    else if(val==0.5)
        {
            print "E"
        }
    else
        {
            print "D" 
        }
    exit
    }
    { 
        p=$1 
    }' filetest.csv

How can we loop through the values in two columns and perform control statement?

PS: the criteria for my if-else statement is (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).

2
  • your first expected value 0.654 is bigger than 0.65, that's why it doesn't fit any of your conditions Commented May 5, 2017 at 8:41
  • actually the values doesn't matter - i will fix that now. Commented May 5, 2017 at 8:42

1 Answer 1

1

Your first expected value 0.654 is bigger than 0.65(the 3rd condition as the closest one), that's why it doesn't fit any of your conditions.

Extend your script as shown below:

awk '$1<p{ 
    val=$1/$2  # to divide the first column value on the second column value              
    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

The output:

C

The crucial line I've modified is:

val=$1/$2  # to divide the first column value on the second column value
Sign up to request clarification or add additional context in comments.

14 Comments

shouldn't the expression for "p" be changed (or modified)?
@Mahsolid, why should it? I have fixed your script according to your input description
oh, sorry. i got it now and thanks. Accepted answer!
by the way, when i try to run it - i got this error awk: cmd. line:2: (FILENAME=filetest.csv FNR=23) fatal: division by zero attempted
That could be. We can debug that moment. Let's print those values before the division: print $1, $2; val=$1/$2. What is the debug output?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.