MKF
MKF

Reputation: 45

chose row and column value based on condition

I have a table called resultat.csv and it would look like this :

  20.7  120.4  -80
 -10.1   78   -155
  5.44  32.4   43.9

What I tried to do is :

on each line of resultat.csv

if the value of the 1st column is less or equal to 0 then you pick the 
value from column 3

ELSE

if the value of the 1st column is greater than 0 then you pick the 
value from column 2

The value of the 1st column is a decimal number that can be negative.

Here my code :

 awk -F " " ' {print $1}' resultat.csv | while IFS=, read -r k
do
    status=$(echo "$k <= 0" | bc)

if [[ $status -eq 1 ]];then

grep -- $k resultat.csv | awk -F ' ' '{print $3}' 

elif [[ $status -eq 0 ]];then

grep -- $k resultat.csv | awk -F ' ' '{print $2}' 

fi

done > Y.csv

But it doesn't give error but it doesn't give my expected output

120.4
-155
32.4

the grep I use doesn't catch the value from the 1st column of resultat.csv but all the value what look like.

Any suggestion please ?

Upvotes: 1

Views: 44

Answers (1)

Shawn
Shawn

Reputation: 52344

Just do it all in awk to keep it simple.

awk '{ if ($1 > 0) print $2; else print $3 }' resultat.csv

Upvotes: 1

Related Questions