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



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-31-2004
raguramtgr raguramtgr is offline
Registered User
  
 

Join Date: Jan 2004
Location: chennai, india
Posts: 47
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
  #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 07:25 AM..
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 06:13 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0