Amit
Amit

Reputation: 129

comparing all the values of a column if condition is met

I have a file with 4 columns. Columns 2 and 4 have numbers.

a 55  k 3
b 59 l 3
c 79 m 277
d 255 n 277
e 257 o 267
f 267 p 287
g 290  q 287
h 290 r 287
i 310 s 900

I need to compare all the rows of column 2 with those in column 4, not limited to same rows, and select only those rows in column 4, where values in column 4 are greater than those in column 2 in a range of 1 to 30. For example, row 3 in column 4 will also be selected because it has difference in same range with values in column 2 in rows 4,5,6. A possible output from sample file fulfilling above condition may be like below.

m 277
n 277
o 267
p 287
q 287
r 287

Sorry if I did not present my problem clearly, thank you in advance.

Upvotes: 0

Views: 161

Answers (1)

SheetJS
SheetJS

Reputation: 22925

If I understand correctly, you want

awk '{for(i=1;i<=30;++i) feasible[i+$2]=1;} {val[NR] = $4; letter[NR] = $3} END {for(y in val) if(feasible[val[y]]) print letter[y], val[y]}'

Basically, you keep track of all feasible values for the fourth column. You also keep track of the third and fourth columns of data. Then, at the end, you check each value and see if they are feasible

Upvotes: 4

Related Questions