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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk script -print line when $2 > $2 of previous line
# 1  
Old 03-05-2013
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 ...

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

Code:
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
# 2  
Old 03-05-2013
Try:
Code:
awk '!a[$1]++||($2-old[$1] > 30){print}{old[$1]=$2}'

Based on the logic you described, you are missing line
Code:
AN134      338    426496 1906          1 UNE    Cefaclor

in your sample output, or...?
This User Gave Thanks to mirni For This Post:
# 3  
Old 03-05-2013
Works beautifully thank you!!
-man that was fast!!!

... my sample output was wrong sorry. My mistake. Your code nailed it though. Cheers!
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 or sed to print the character from the previous line after the regexp match

Hi All, I need to print the characters in the previous line just before the regular expression match Please have a look at the input file as attached I need to match the regular expression ^ with the character of the previous like and also the pin numbers and the output file should be like... (6 Replies)
Discussion started by: kshitij
6 Replies

2. AIX

Print nth previous line after match

Please help me print nth line after match awk or sed one line command. (3 Replies)
Discussion started by: sushma123
3 Replies

3. Shell Programming and Scripting

How to print previous line of multiple pattern matched line?

Hello, I have below format log file, Comparing csv_converted_files/2201/9747.1012H67126.5077292103609547345.csv and csv_converted_files/22019/97447.1012H67126.5077292103609547345.csv Comparing csv_converted_files/2559/9447.1012H67126.5077292103609547345.csv and... (6 Replies)
Discussion started by: arvindshukla81
6 Replies

4. Shell Programming and Scripting

Print a field from the previous line

plz help me!! I have this file , 3408 5600 3796 6035 4200 6285 4676 0 40 1554 200 1998 652 2451 864 2728 1200 0 I want it like if $2==0,replace it with field from the previous line+500 say here the o/p would be like 3408 5600 3796 6035 4200 6285... (16 Replies)
Discussion started by: Indra2011
16 Replies

5. UNIX for Dummies Questions & Answers

Awk to print data from current and previous line

Hi guys, I have found your forum super useful. However, right now I am stuck on a seemingly "simple" thing in AWK. I have two columns of data, the first column in Age (in million years) and the second column is Convergence Rate (in mm/yr). I am trying to process my data so I can use it to... (2 Replies)
Discussion started by: awk_noob_456
2 Replies

6. Shell Programming and Scripting

awk get matched line's previous line

hi everyone, a b in c d e f in g output is: a e so awk search for "in", then print out the matched line's previuos line. Please advice. (11 Replies)
Discussion started by: jimmy_y
11 Replies

7. Shell Programming and Scripting

Print the previous line

My requirement is that when ever search criteria matchs in log file, the previous line just above the search word as well as search word should be print. sample log file --03-19T11:26 xxx create version "a.sh@@/main/6" "104157 " --03-18T16:01 xxx create version "a.sh@@/main/5" ... (6 Replies)
Discussion started by: jadoo_c2
6 Replies

8. Shell Programming and Scripting

Awk script to create new file based on previous line

Need help creating a script that does the following: Sort a file Compare the previous line "last field" with current line "last field" If they are the same, print output to a file If they are different print output to a new file The script should keep creating new files if the previous... (5 Replies)
Discussion started by: Muga801
5 Replies

9. Shell Programming and Scripting

how Print previous line ..........

HELLO...I wanted to ask you, than sure know unix more than me, as I can obtain the following solution: I have a file with rows of the type: CIAO COME STAI PERCHE COME STAI CIAO COME VA ALLO CHE FACCIAMO ................. I would that if in a line is present the word (for example) " CHE... (9 Replies)
Discussion started by: fabi20
9 Replies

10. Shell Programming and Scripting

awk;sed appending line to previous line....

I know this has been asked before but I just can't parse the syntax as explained. I have a set of files that has user information spread out over two lines that I wish to merge into one: User1NameLast User1NameFirst User1Address E-Mail:User1email User2NameLast User2NameFirst User2Address... (11 Replies)
Discussion started by: walkerwheeler
11 Replies
Login or Register to Ask a Question