![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !! |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| sed query | gopsman | Shell Programming and Scripting | 4 | 02-03-2009 02:41 AM |
| & in SQL query | Lindarella | Shell Programming and Scripting | 3 | 10-09-2006 04:43 PM |
| Query In awk | raguramtgr | UNIX for Dummies Questions & Answers | 1 | 08-27-2004 11:00 AM |
| RPM Query | silvaman | UNIX for Advanced & Expert Users | 1 | 08-15-2003 07:56 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
Query in awk
file "batsmen" contains
Bradman;Australia;52;6996 Gavaskar;India;124;10122 Sobers;West;Indies;93;8032 Border;Australia;109;8075 Miandad;Pakistan;97;7121 Gower;England;94;7023 Boycott;England;108;8114 My awk script contains awk '{FS=";"} {len=length($1)}; {printf("%s %d\n"), $1, len}' batsmen when i run the script i get an output like this Bradman;Australia;52;6996 25 Gavaskar 8 Sobers 6 Border 6 Miandad 7 Gower 5 Boycott 7 i need the first row as "Bradman 7" but it is not working with this script. What should i do to rectify this. More over when to use "BEGIN & END" replies appreciated. raguram r |
|
|||||
|
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 07:25 AM.. |
| Sponsored Links | ||
|
|