#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Posts
    1
    Rep Power
    0

    BEGIN and END format in awk


    I'm new to awk, trying to understand the basics.
    I'm trying to reset the counter everytime the program gets a new file to check.
    I figured in the BEGIN part it would work, but it doesn't.
    Would appreciate your help.

    Code:
    #!/bin/awk -f 
    BEGIN   {counter=0}           
           {
              sum=0         
              for ( i=1; i<=NF; i++){   
                   sum += $i       
              }            
         if ( sum == copy) {                
                   counter += 1           
          }             
        copy=sum     
        } 
    END   {     
        counter+=1      
       print counter     
        if(counter == NR) 
       {    
                 print FILENAME " " "YES" " " sum      
       }     
        else     
        {         
            print FILENAME " " "NO" " " sum     
        } 
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,451
    Rep Power
    1751
    I believe the BEGIN and END code blocks get executed at the start and end of the awk process, not on an individual file basis.

    I'd be inclined to do (not tested!)

    Code:
    awk 'BEGIN {oldfile = ""; sum = 0; counter = 0 }
      {
        if (oldfile != FILENAME ) {
           counter++;
           sum = 0;
           if (oldfile != "" ) {
    # print details on previous file
           }
           oldfile = FILENAME;
        }
        else {
    # do what you need to do on a per row, per file basis
        }
        END {
    # print details of last file
        }
      }'
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,451
    Rep Power
    1751
    And now I am awake and had a coffee, having thought about this (still not tested!) a slightly more elegant way might be:
    awk 'BEGIN {oldfile = FILENAME; sum = 0; counter = 0 }
    {
    if (oldfile != FILENAME ) {
    # print details on previous file
    counter++;
    sum = 0;
    oldfile = FILENAME;
    }
    else {
    # do what you need to do on a per row, per file basis
    }
    END {
    # print details of last file
    }
    }'
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,396
    Rep Power
    1871
    > FNR ordinal number of the current record in the current file
    This equals 1 at the start of every file.

    This might be better, since it allows you to process the same file twice (which testing FILENAME would barf on).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo