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?
# 8  
Old 02-11-2013
Quote:
in front of the x assignment
# 9  
Old 02-11-2013
Sir I think I am doing something wrong here, as I do program most of time in assembly language, like that I tried to clear x and y after the end of internal loop...but its resulting something else, I really have no idea that whats wrong here...same logic is working in FORTRAN with do loop and label for repetition.

---------- Post updated at 04:45 AM ---------- Previous update was at 04:26 AM ----------

I didn't get it sir..if you have time...please help me in modifying it.

---------- Post updated at 05:06 AM ---------- Previous update was at 04:45 AM ----------

You might have remember I had posted this before also..
Code:
do 20 i=1,217      
        x=0
        y=0
        do 21 j=1,2440
        if(column2_1(j).ge.column1_1(i)-0.1.and.column2_1(j).le.column1_1(i)+0.1)then
        if(column1_1(i).ge.column1_2(j))then
        x=x+1
        endif        
        if(column1_1(i).le.column1_2(j))then
        y=y+1
        endif
        endif
 21     continue      
        if(x.ge.1.and.y.ge.1)then
        write(13,'(a70)')dat(i)
        endif       
 20     continue 
    stop
    end

# 10  
Old 02-11-2013
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]
        }

resulting in
Code:
END     {for (i=1;i<=217;i++)                                       # For loop 2nd File 1st and 2nd column
           {x=0                                                     # Intialize x and y value 0
            y=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]
        }

As you reset x and y in every iteration, your final if will consider the last iteration's values only. So I guess you should reverse those lines as well:
Code:
            if(x>=1 && y>=1) print i, j, A[2,i,1], A[2,i,2]
           }                                                        # Internal loop ends here
        }

This User Gave Thanks to RudiC For This Post:
# 11  
Old 02-11-2013
Thanks a lot, its printing, now I need to verify with FORTRAN result.
I wish it will be fine...

and one more thing I wanted to ask you, for inner loop I want to assign NR of file 1 and for outer loop NR of fie2,

Code:
if I assign simply for(i=1;i<=NR;i++)

which NR it will take ? by default

Code:
c=`awk 'END{print NR}' file1`
d=`awk 'END{print NR}' file2`
for(i=1;i<='$c';i++)

till date I am assigning counter value from bash variable like above

Thanks a lot RudyC, for using your valuable time to help me....I just processed 413513 lines, 5855826 characters fille

Last edited by Akshay Hegde; 02-11-2013 at 08:27 AM.. Reason: to thank my God RudyC
# 12  
Old 02-12-2013
For your file[12] line count, in the first lines of the program try sth. like
Code:
FNR==1 {i++}
       {LC[i]=NR}

The respective LC element will hold the last NR for each file.

Last edited by RudiC; 02-12-2013 at 03:59 AM..
This User Gave Thanks to RudiC For This Post:
# 13  
Old 02-12-2013
Thank you so much sir...for solving my big problem,

Sir I am trying one more thing with same file to display unmatched records

my problem is like this.. for instance say file1 has 1000 records, file 2 has 750 records, file1's 750 records are matching with file 2

I want to display remaining 250 records from file 1

I could able to display matching records, using following script

Code:
awk 'FNR==1{i++}{for(k=1;k<=NF;k++) A[i,FNR,k]=$k}
END{
for(i=1;i<='$d';i++)
{
for(j=1;j<='$c';j++)
if(A[2,i,2]==A[1,j,1] && A[2,i,1]==A[1,j,2])
print A[2,i,2], A[2,i,1], "<==F2 equal F1==>", A[1,j,1],A[1,j,2]
}}' OFS="\t" file1 file2

# 14  
Old 02-12-2013
Not sure I understand. If the first 750 records in both files match and you want to display records 751 - 1000 of file1, simply reverse the files' order in awk's file list and insert sth. like FNR>LC[1] into your program.
If the mismatches occur anywhere in the files, use the unix diff command.
This User Gave Thanks to RudiC For This Post:
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