awk script -print line when $2 > $2 of previous line

Hi all,

From a while loop I am reading a sorted file where I want to print only the lines that have $1 match and $2 only when the difference from $2 from the previous line is > 30.

Input would be like ...

AN237      010    193019 0502          1 CSU    Amoxycillin   
AN237      080    193019 0502          1 CSU    Gentamicin    
AN237      096    193019 0502          1 CSU    Nitrofurantoin
AN237      183    193019 0502          1 CSU    Norfloxacin   
AN134      238    426496 1906          1 UNE    Amoxycillin+Cl
AN134      238    426496 1906          1 UNE    Amoxycillin   
AN134      338    426496 1906          1 UNE    Cefaclor      
AN134      338    426496 1906          1 UNE    Flucloxacillin
AN134      343    426496 1906          1 UNE    Gentamicin    
AN202      011    230443 1311          1 ULC    Cotrimoxazole 
AN202      019    230443 1311          1 ULC    Erythromycin  
AN202      020    230443 1311          1 ULC    Flucloxacillin
AN202      123    230443 1311          1 ULC    Penicillin    
AN202      133    230443 1311          1 ULC    Tetracycline  
AN202      186    249176 1311          1 ABS    Cotrimoxazole 
AN202      193    249176 1311          1 ABS    Erythromycin  
AN202      297    514219 1311          1 ULC    Cotrimoxazole 
AN202      297    514219 1311          1 ULC    Erythromycin 

... the desired output being ...

AN237      010    193019 0502          1 CSU    Amoxycillin   
AN237      080    193019 0502          1 CSU    Gentamicin    
AN237      183    193019 0502          1 CSU    Norfloxacin   
AN134      238    426496 1906          1 UNE    Amoxycillin+Cl
AN202      011    230443 1311          1 ULC    Cotrimoxazole 
AN202      123    230443 1311          1 ULC    Penicillin    
AN202      186    249176 1311          1 ABS    Cotrimoxazole 
AN202      297    514219 1311          1 ULC    Cotrimoxazole

So a couple of things need to be handled at once in this script.
Any idea?
Cheers, g

Try:

awk '!a[$1]++||($2-old[$1] > 30){print}{old[$1]=$2}'

Based on the logic you described, you are missing line

AN134      338    426496 1906          1 UNE    Cefaclor      

in your sample output, or...?

1 Like

Works beautifully thank you!!
-man that was fast!!!

... my sample output was wrong sorry. My mistake. Your code nailed it though. Cheers!