AWK Looping. How can I get expected result?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK Looping. How can I get expected result?
# 1  
Old 02-11-2013
AWK Looping. How can I get expected result?

Can Anyone help with looping...

Code:
awk 'FNR==1{i++} 
{for(k=1; k<=NF; k++) A[i,FNR,k]=$k}                                # 3 Dimension Array 
END{
for(i=1;i<=217;i++)                                                             # For loop 2nd File 1st and 2nd column
x=0;y=0                                                                           # Intialize x and y value 0
{
for(j=1;j<=2440;j++)                                                   # For loop for 1st file 1st and 2nd column
if(A[1,j,2]>=A[2,i,2]-0.1 && A[1,j,2]<=A[2,i,2]+0.1)            # Comparison starts here
{
if(A[1,j,1]>=A[2,i,1])x=x+1;                           
if(A[1,j,1]<=A[2,i,1])y=y+1;
}                                  
}                                                                             # Internal loop ends here
if(x>=1 && y>=1)
print i,j,A[2,i,1],A[2,i,2]}' OFS="\t" file1.pl file2.pl                  # 1st for loop ends here and printing


Result : coming
218 2441 75.5 10.07

Expected result :
1 2441 75.5   10.05
2 2441 75.02  10.15
3 2441 74.52  10.23


Last edited by Akshay Hegde; 02-11-2013 at 03:36 AM.. Reason: modify counter
# 2  
Old 02-11-2013
It helps allot if you also post the input data.
# 3  
Old 02-11-2013
Dear Jotne Kindly look at attachment

Last edited by Akshay Hegde; 02-11-2013 at 03:35 AM..
# 4  
Old 02-11-2013
And how do you like to calculate this two file to get your result?
# 5  
Old 02-11-2013
problem here is I could not able to trace where internal loop is ending and where outer loop is ending .

here it was suppose to do like this repeat following statements 2440 times, after that come out of loop, print if statement is true else increment outer loop(i).. again repeat following statements 2440 times...

Code:
if(A[1,j,2]>=A[2,i,2]-0.1 && A[1,j,2]<=A[2,i,2]+0.1)            # Comparison starts here
{
if(A[1,j,1]>=A[2,i,1])x=x+1;                           
if(A[1,j,1]<=A[2,i,1])y=y+1;
}

Code:
instead of printing result after every increment of i its printing result at last value of i (thats i=218)
except 
Code:
if(x>=1 && y>=1) is false

I hope you will understand

This is that logic, I had posted earlier unfortunately nobody helped, Thank God at least you asked me..what you want to do

1 . First read column 1 and column 2 of file1.plo store it in array(reference)

2 . Now read column 1 and column 2 of file2.plo store it in another array,to be compared with reference

3 . for loop : - for(i=1;i<=file2.plo_length;i++)

4 . x=0; y=0 this will get clear as and when i will increment

5 . nested loop :- for(j=1;j<=file1.plo_lenght;j++)

6 . if(col1_f1[j]>=col1_f2-0.1[i] && col1_f1[j]<=col1_f2+0.1[i])

if above if statement is true then,go to following statement else check condition with j++

7 . if(col2_f2[i]>=col2_f1[j]) then x=x+1

8 . if(col2_f2[i]<=col2_f1[j]) then y=y+1

repeat 5,6,7 and 8 till j reaches file1.txt_length(NR),Once j reaches NR following

9 . if(x>=1 && y>=1) then print column1_f2[i] column2_f2[i]

repeat statements after number 4 to 9 till i reaches file2.plo_length(NR)

Last edited by Akshay Hegde; 02-11-2013 at 04:56 AM..
# 6  
Old 02-11-2013
What immediately jumps to my eyes (after rearranging / indenting your code for better readability) is that your first for loop sets x=0 217 times, then y=0 once, then continues to the rest:
Code:
END     {for(i=1;i<=217;i++)                                                    # For loop 2nd File 1st and 2nd column
         x=0;y=0                                                                # Intialize x and y value 0
                 {for(j=1;j<=2440;j++)                                          # For loop for 1st file 1st and 2nd column
                    if(A[1,j,2]>=A[2,i,2]-0.1 && A[1,j,2]<=A[2,i,2]+0.1)        # Comparison starts here
                        {if(A[1,j,1]>=A[2,i,1])x=x+1;                           
                         if(A[1,j,1]<=A[2,i,1])y=y+1;
                        }                            
                 }                                                              # Internal loop ends here
         if(x>=1 && y>=1) print i, j, A[2,i,1], A[2,i,2]
        }

You want to move that second opening brace in front of the x assignment and retry...
# 7  
Old 02-11-2013
Do you mean like this
Code:
END {for(i=1;i<=217;i++)x=0{for(j=1;j<=2440;j++)

I want to clear x and y value after the end of internal j loop

Sir I still not getting result as I expected

Last edited by Akshay Hegde; 02-11-2013 at 05:15 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep output to file result not as expected

Hi Gurus, I run command grep ABC file1 > file2 against below file. I got all ABC_xxx in one line in file2. I expect to get multiple lines in file2. If I print result in screen, the result is expected. thanks in advance My os is SunOS 5.10 Generic_150400-64 sun4v sparc sun4v ABC_123 XXXXX... (2 Replies)
Discussion started by: green_k
2 Replies

2. Shell Programming and Scripting

Not getting expected result

Hi Experts, I have written the below script but its not working as per expectation. My requirement is if this condition ] is satisfied then only check for this condition ] if this also satisfied check for the condition ]. vi p_values.ksh path="/db/ora/files" mode=1 b_days=10... (5 Replies)
Discussion started by: nalu
5 Replies

3. Shell Programming and Scripting

Assigning variable to output gives error with expected result

Hello, I am trying to print out the first string matching query with grep and I need your help. My scenario: Database John F 4433 Street No 88 CA Elisabeth Taylor 7733 Street No 26 ON Jack Nicholson 0133 Green Park No 34 AR John F 2 9399 Southpark No 02D UT test.sh... (6 Replies)
Discussion started by: baris35
6 Replies

4. Shell Programming and Scripting

Help on looping using awk

I have the data like this: PONUMBER,SUPPLIER,LINEITEM,SPLITLINE,LINEAMOUNT,CURRENCY IR5555,Supplier1,1,1,83.1,USD IR5555,Supplier1,1,3,40.4,USD IR5555,Supplier1,1,6,54.1,USD IR5555,Supplier1,1,8,75.1,USD IR5556,Supplier2,1,1,41.1,USD IR5556,Supplier2,1,3,43.1,USD ... (3 Replies)
Discussion started by: jeffreybsu
3 Replies

5. Shell Programming and Scripting

Result of Catching Return Value from Sub_script.sh to Main_script.sh is not as Expected

Main_script.sh #! /bin/sh ./Sub_script.sh rc=$? echo "Return code from Sub_script.sh : $rc" if ; then echo "$rc = 991" echo "" exit 1 elif ; then echo "$rc = 992" echo "" exit 1 elif ; then echo "$rc = 0" echo "" exit 1 fi (2 Replies)
Discussion started by: duddukuri
2 Replies

6. Shell Programming and Scripting

looping in awk

How do I remove last comma? echo "xx yy zz" | awk 'BEGIN{FS=" "}{for (i=1; i<=NF; i++) printf "%s,", $i}'output: xx,yy,zz, required output: xx,yy,zz or (ideally!): xx, yy & zz many thanks in advance! (4 Replies)
Discussion started by: euval
4 Replies

7. Programming

Test program not giving expected result

I have five classes. 2 composition classes,1 aggregation class and 1 dependency class.I have coded all the classes but one of my test program is not giving me the expected result.I have the following classes: TimeStamp Interval (composition of 2 TimeStamps) TimeSheet ( aggregation of many... (3 Replies)
Discussion started by: moraks007
3 Replies

8. UNIX for Dummies Questions & Answers

Help with AWK looping

I'm trying to parse a configuration text file using awk. The following is a sample from the file I'm searching. I can retrieve the formula and recipe names easily but now I want to take it one step farther. In addition to the formula name, I would like to also get the value of the attribute... (6 Replies)
Discussion started by: new2awk
6 Replies

9. UNIX for Advanced & Expert Users

executing script by cron doesnt give me expected result

Hi frnds... I m facing very irritating problem already waisted my 2 days.. I have a following script..( i am pasting only the main code) ftp -ivn 213.194.40.77 <<FTP user $user $password binary cd $FileDir/out lcd $localpath get $file rename $FileDir/out/$file $FileDir/tmp/$file... (1 Reply)
Discussion started by: clx
1 Replies

10. UNIX for Advanced & Expert Users

Looping in awk

Can somebody give me a cleaner way of writing the following script. I was thinking that I could use a loop in the awk statement. It works fine the way it is but I just want the script to be cleaner. #!/usr/bin/sh for r in 0 1 2 3 4 5 6 do DAY=`gdate --date="${r} days ago" +%m\/%d\/%y`... (3 Replies)
Discussion started by: keelba
3 Replies
Login or Register to Ask a Question