How to capture 2 consecutive rows when a condition is true ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to capture 2 consecutive rows when a condition is true ?
# 15  
Old 10-31-2006
Hi anbu, i have not miss out anything. Same error keeps popping up. Pls help
# 16  
Old 10-31-2006
Your code seems correct.
Try to execute the script with sh instead of csh :
Code:
#!/bin/sh

nawk '
$1 ~ "^[0-9][0-9]*$" && $NF == 0 { grp1=grp1+$(NF-1); tot1=tot1+1 }
$1 ~ "^[0-9][0-9]*c$" && $NF == 0 { grp2=grp2+$(NF-1); tot2=tot2+1 }
END {
printf("Group1\nAverage = %f \n",grp1/tot1)
printf("Group2c\nAverage = %f\n",grp2/tot2)
}' xxx.txt

Another test, put your awk program in a separate file:
Code:
$cat testing1.awk
$1 ~ "^[0-9][0-9]*$" && $NF == 0 { grp1=grp1+$(NF-1); tot1=tot1+1 }
$1 ~ "^[0-9][0-9]*c$" && $NF == 0 { grp2=grp2+$(NF-1); tot2=tot2+1 }
END {
printf("Group1\nAverage = %f \n",grp1/tot1)
printf("Group2c\nAverage = %f\n",grp2/tot2)
}
$cat testing1
nawk -f testing1.awk xxx.txt


Jean-Pierre.
# 17  
Old 10-31-2006
Hi Jean, thanks a million, it works!!
# 18  
Old 10-31-2006
Hi, Just notice something. If the input does not contain any 1st field with "c" appending to the number, it will show the below error. Notice also the average value is wrong. I think its because both line for eg 0001 contain "0" in the last field and i suppose it better not to use "0" as the reference point. i use 2nd field "tt=" instead. So i modified the main code to the below.

$1 ~ "^[0-9][0-9]*$" && $2 ~ "tt=" { grp1=grp1+$(NF-1); tot1=tot1+1 }
$1 ~ "^[0-9][0-9]*c$" && $2 ~ "tt=" { grp2=grp2+$(NF-1); tot2=tot2+1 }
END {
printf("Group1\nAverage = %f \n",grp1/tot1);
printf("Group2c\nAverage = %f\n",grp2/tot2)
}


For Example, my input is below

0001 x= 1 $-----.--.-..---.-..----..-.------ 0
0001 tt= 173 358 461 703 907 959 969 1151 1313 1876 2398 0

0002 x= 1 $-----.--.-..---.-..----..-.---------.--. 0
0002 tt= 167 353 455 698 900 952 962 1145 1306 1869 2391 0


Output will have error as shown below

Group1
Average = 2394.500000
nawk: division by zero
input record number 41, file xxx.txt
source line number 5


But how do i modify the above awk code such that when the input contains only one of each type (refering to group 1 or group 2 type only), it will not show any error ?

Thanks

Last edited by Raynon; 10-31-2006 at 09:39 PM..
# 19  
Old 10-31-2006
Code:
if(tot1 != 0 ) printf("Group1\nAverage = %f \n",grp1/tot1)
if(tot2 != 0 ) printf("Group2c\nAverage = %f\n",grp2/tot2)

# 20  
Old 10-31-2006
Quote:
Originally Posted by anbu23
Code:
if(tot1 != 0 ) printf("Group1\nAverage = %f \n",grp1/tot1)
if(tot2 != 0 ) printf("Group2c\nAverage = %f\n",grp2/tot2)

Thanks anbu!! It works just fine!
# 21  
Old 11-03-2006
Hi , Just another question. Why is it that only 2 sets of [0-9] is needed eg [0-9][0-9] instead of 4 sets that is [0-9][0-9][0-9][0-9] since it is a 4-digit number ?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to capture lines that meet either condition

I am trying to modify and understand an awk written by @Scrutinizer The below awk will filter a list of 30,000 lines in the tab-delimited file. What I am having trouble with is adding a condition to SVTYPE=CNV that will only print that line if CI=,0.95: portion in blue in file is <1.9. The... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

Compute value from more than three consecutive rows

Hello all, I am working on a file like below: site Date time value1 value2 0023 2014-01-01 00:00 32.0 23.7 0023 2014-01-01 01:00 38.0 29.9 0023 2014-01-01 02:00 85.0 26.6 0023 2014-01-01 03:00 34.0 25.3 0023 2014-01-01 04:00 37.0 23.8 0023 2014-01-01 05:00 80.0 20.3 0023 2014-01-01 06:00... (16 Replies)
Discussion started by: kathy wang
16 Replies

3. Shell Programming and Scripting

Average across rows with a condition

Hi Friends, My input file Gene1 10 20 0 Gene2 5 0 15 Gene3 10 10 10 Gene4 5 0 0 If there is a zero for any gene in any column, I don't want that column to be considered which reduces the denominator value during average. Here is my output Gene1 10 20 0 10 Gene2 5 0 15 10 Gene3... (5 Replies)
Discussion started by: jacobs.smith
5 Replies

4. Shell Programming and Scripting

Columns to Rows - Transpose - Special Condition

Hi Friends, Hope all is well. I have an input file like this a gene1 10 b gene1 2 c gene2 20 c gene3 10 d gene4 5 e gene5 6 Steps to reach output. 1. Print unique values of column1 as column of the matrix, which will be a b c (5 Replies)
Discussion started by: jacobs.smith
5 Replies

5. Shell Programming and Scripting

Convert rows to columns based on condition

I have a file some thing like this: GN Name=YWHAB; RC TISSUE=Keratinocyte; RC TISSUE=Thymus; CC -!- FUNCTION: Adapter protein implicated in the regulation of a large CC spectrum of both general and specialized signaling pathways GN Name=YWHAE; RC TISSUE=Liver; RC ... (13 Replies)
Discussion started by: raj_k
13 Replies

6. Shell Programming and Scripting

Capture rows for a column in file from delete sql -Oracle

Hi, This may not be the right forum but i am hoping someone knows an answer to this. I have to capture rows for a column that was deleted. How can i do that without having to write a select query? delete from myschema.mytable where currentdatetimestamp > columnDate this should delete 5... (4 Replies)
Discussion started by: jakSun8
4 Replies

7. Shell Programming and Scripting

deleting rows under a certain condition

there are 20 variables and I would like to delete the rows if 13th-20th columns are all NA. Thank you! FID IID aspirpre statihos fibrahos ocholhos arbhos betabhos alphbhos cacbhos diurehos numbcig.x toast1 toast2 toast3 toast4 ischoth1 ischoth2 ischoth3 ischoth4 101 101 1 1 1 1 1 2 1 2... (2 Replies)
Discussion started by: johnkim0806
2 Replies

8. Shell Programming and Scripting

Print merged rows from two files by applying if condition

Hi all, I have list of two kind of files and I want to compare the rows and print the merged data by applying if condition. First kind of file looks like: and second kind of file looks like : I want to print the rows present in second file followed by 3 more columns from first... (6 Replies)
Discussion started by: CAch
6 Replies

9. Shell Programming and Scripting

remove consecutive duplicate rows

I have some data that looks like, 1 3300665.mol 3300665 5177008 102.093 2 3300665.mol 3300665 5177008 102.093 3 3294015.mol 3294015 5131552 102.114 4 3294015.mol 3294015 5131552 102.114 5 3293734.mol 3293734 5129625 104.152 6 3293734.mol ... (13 Replies)
Discussion started by: LMHmedchem
13 Replies

10. UNIX for Dummies Questions & Answers

how to capture no. of rows updated in update sql in unix db2

hi, i am a new user in unix..and we have unix db2. i want to capture the no. of rows updated by a update db2 sql statement and redirect into a log file. I've seen db2 -m...but not sure how the syntax should be. The update sql that I'm going to run is from a file... Can you please share... (1 Reply)
Discussion started by: j_rymbei
1 Replies
Login or Register to Ask a Question