Comparing all lines in a column with another is condition is met


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Comparing all lines in a column with another is condition is met
# 1  
Old 06-24-2013
Comparing all lines in a column with another is condition is met

Sorry for this noob question,
I have file with 4 columns like where columns 2 and 4 have numbers

Code:
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

now i want to select only those rows, where values in column 4 are greater than those in column 2 in a range of 1 to 30.
so the out put should be like

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

and those which exactly have a difference of 10
Code:
m 277
n 277
o 267

I will appreciate any help. I hope this can be done with awk.
Thanks

Last edited by amits22; 06-24-2013 at 09:08 PM.. Reason: Changed HTML tags to CODE tags
# 2  
Old 06-24-2013
Quote:
Originally Posted by amits22
Sorry for this noob question,
I have file with 4 columns like where columns 2 and 4 have numbers

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

now i want to select only those rows, where values in column 4 are greater than those in column 2 in a range of 1 to 30.
so the out put should be like

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

and those which exactly have a difference of 10
Code:
m 277
n 277
o 267

I will appreciate any help. I hope this can be done with awk.
Thanks
I can't understand what you're trying to do. There are several problems here including:
  1. You said your input has four columns, but the first line of your input only has two columns.
  2. You said you "want to select only those rows, where values in column 4 are greater than those in column 2 in a range of 1 to 30", but in your 1st line showing what the output should be, 79 is not in the range 1 to 30; 277 is not in the range 1 to 30; and (277 - 79) is not in the range 1 to 30.
  3. And, then for the second output file, you said you wanted to select lines from the first output file "which exactly have a difference of 10", but in the first line of output you show, 79 is not in the range 1 to 30; 277 is not in the range 1 to 30; (277 - 79) is not in the range 1 to 30; and (277 - 79) is certainly not 10.
Please give us a clearer statement of your requirements and give us examples that match your requirements!
# 3  
Old 06-24-2013
Hi Don, sorry for not being clear.
1. Sorry that was a sloppy way of showing sample data Smilie. It now has 4 columns

2. and 3. I need to compare all the rows of column 4 with all the rows of column 2, comparison is not limited to same rows. Which is why i printed 3rd row of column 3 and 4 because '277' in column 4 has a difference in range of 1-30 from values in column 2 in rows 4,5 etc.

Please do let me know if this question does not still make a clear sense.

Thank you.

Last edited by amits22; 06-24-2013 at 09:11 PM..
# 4  
Old 06-24-2013
Here is a simple brute force awk script that I think does what you want:
Code:
awk '
FNR == NR {
        for(i = ($2 + 1); i <= ($2 + 30); i++) r1to30[i]
        r10[$2 + 10]
        next
}
$4 in r10 {
        print $3, $4 > "out10"
}
$4 in r1to30 {
        print $3, $4 > "out1-30"
}' file file

It produces two output files: out10 contains columns 3 and 4 of the input file lines where column 4 is 10 greater than some value in column 2 of the input file, and out1-30 contains columns 3 and 4 of the input file lines where column 4 - some value in column 2 is greater than or equal to 1 and less than or equal to 30.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 06-25-2013
amazing, never thought it could be used this way

Quote:
Originally Posted by Don Cragun
Here is a simple brute force awk script that I think does what you want:
Code:
awk '
FNR == NR {
        for(i = ($2 + 1); i <= ($2 + 30); i++) r1to30[i]
        r10[$2 + 10]
        next
}
$4 in r10 {
        print $3, $4 > "out10"
}
$4 in r1to30 {
        print $3, $4 > "out1-30"
}' file file

It produces two output files: out10 contains columns 3 and 4 of the input file lines where column 4 is 10 greater than some value in column 2 of the input file, and out1-30 contains columns 3 and 4 of the input file lines where column 4 - some value in column 2 is greater than or equal to 1 and less than or equal to 30.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk - print when condition is met

I have a file.txt containing the following: Query= HWI-ST863:386:C5Y8UACXX:3:2302:16454:89688 1:N:0:ACACGAAT Length=100 Score E Sequences producing significant alignments: (Bits) Value ... (2 Replies)
Discussion started by: tons92
2 Replies

2. Shell Programming and Scripting

Add another condition to bash for when not met

In the below I can not seem to add a line that will add Not low if the statement in bold is not true or meet. I guess when the first if statement is true/meet then print low, otherwise print Not low in $(NF + 1). I am not sure how to correctly add this. Thank you :). if(low <= $2 && $2 <=... (5 Replies)
Discussion started by: cmccabe
5 Replies

3. Shell Programming and Scripting

Need help on how to append on the filename when condition met.

Hi All, Seeking for your assistance on how to append the specific string when $3 condion met. ex. file1.txt ar0050046b16,5,888,0,0,0,0.00,0.00,0.00,0.00,25689.55 ar0050046b16,5,0,0,0,0,0.00,0.00,0.00,0.00,25689.55 ar0050046b16,5,0,0,0,0,0.00,0.00,0.00,0.00,25689.55 expected output:... (5 Replies)
Discussion started by: znesotomayor
5 Replies

4. Shell Programming and Scripting

Getting the records once condition met

Hi All, Seeking for your assistance to get the records once the $2 met the condition. Ex. file 1.txt 123455,10-Aug-2020 07:33:37 AM,2335235,1323534,12343 123232,11-Aug-2015 08:33:37 PM,4234324,1321432,34364 Output: 123455,10-Aug-2020 07:33:37 AM,2335235,1323534,12343 What i did... (5 Replies)
Discussion started by: znesotomayor
5 Replies

5. Shell Programming and Scripting

Delete if condition met in a column

i have a table like this: id, senderNumber, blacklist ----------------------------- 1 0835636326 Y 2 0373562343 Y 3 0273646833 Y and I want to delete automatically if a new inserted row on another table consist anything on senderNumber column above using a BASH Script I... (9 Replies)
Discussion started by: jazzyzha
9 Replies

6. UNIX for Advanced & Expert Users

While loop only if a condition is met

All, I wrote the following section of code (which logically in PHP would of worked): tmpPATH=${1} tmpTAG=${2} if then while read tmpTAG tmpPATH do fi echo $tmpTAG echo $tmpPATH if then done < ./config.cfg fi (4 Replies)
Discussion started by: Cranie
4 Replies

7. Shell Programming and Scripting

do nothing if condition is not met but not exit

Hello all, I created the below script....and it seemed to be working fine. My problem is i want the script to ignore rest of the things if my condition is not met but do not exit.... #!/bin/ksh ########################### ########################### # Set name of the listener, this... (2 Replies)
Discussion started by: abdul.irfan2
2 Replies

8. Shell Programming and Scripting

sed/awk to update 1st column if condition met

Hi, I am trying to update the 1st column of a file but only if it contains a char here is an example of my file 1111aaa 9999 textaaa 22222bbb 9999 textbbb 3333 9999 textccc 444ddd 9999 textddd i would like the output to remove any characters () from... (5 Replies)
Discussion started by: plennon
5 Replies

9. Shell Programming and Scripting

How to break a loop if condition is met

I am having trouble figuring this code I want to grep a text from a file and if it match certain text it break out of the loop or it should continue searching for the text Here is what I have written but it isn't working while true f=`grep 'END OF STATUS REPORT' filename` do if ... (9 Replies)
Discussion started by: Issemael
9 Replies

10. Shell Programming and Scripting

Send email to user when condition met

Hi all, I plan to write a shell script to inform users on their task when certain condition met. example: If a then email user on action a else email user on action b. I'm pretty new in scripting, appreciate any suggestions. Thanks. (4 Replies)
Discussion started by: *Jess*
4 Replies
Login or Register to Ask a Question