The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com




Thread: Query in awk
View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #2 (permalink)  
Old 08-31-2004
google's Avatar
google google is offline Forum Advisor  
Moderator
  
 

Join Date: Jul 2002
Location: Atlanta
Posts: 740
Use BEGIN and END when you want some amount of processing to occur before or after any file processing has occurred. The code that is in the BEGIN statement will be executed exactly 1 time, and it will be executed before the file you are processing is opened. Likewise, the code in the END block is executed exactly 1 time and it is executed after all code in the main program has been executed.

Use the BEGIN statement for example to print off a report Header, and use an END statement to print the report footer.

Your code:

awk '{FS=";"}
{len=length($1)};
{printf("%s %d\n"), $1, len}' batsmen

You get a printout of the files contents because Awks default action is to print.

Using BEGIN/END you can change your code as follows:

awk 'BEGIN {FS= ";"} {printf("%s %d\n", $1, length($1) )} END {printf("File Processing Complete\n") }' batsmen

gAwk Manual is a great source for Awk information.

Last edited by google; 08-31-2004 at 08:25 AM..