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 > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
egrep syntax question DKNUCKLES Shell Programming and Scripting 1 11-18-2007 04:46 PM
Simple Awk Question: If Syntax natdeamer Shell Programming and Scripting 1 09-10-2007 12:53 PM
yet another awk field syntax question prkfriryce Shell Programming and Scripting 4 03-22-2007 01:13 PM
C-shell: variable syntax question alex_5161 SUN Solaris 0 01-30-2007 02:43 PM
Question: non-recursive find syntax alexkav UNIX for Dummies Questions & Answers 6 03-10-2005 07:46 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-28-2004
hcclnoodles hcclnoodles is offline
Registered User
  
 

Join Date: Mar 2002
Posts: 272
awk syntax question

Hi there could someone explain what is happening in the following function/statement for me, im just a little confused

code = 'BEGIN{FS=","}
{
printf ("%-11s,%s%s%s,%07.2f,%14s,%-3s\n",$1,substr($2,9,2),substr($2,6,2),substr($
2,3,2),$9,$10,$12)
}

this function is called later in the script by using

awk "$code"


any guidance would be greatly appreciated

cheers
  #2 (permalink)  
Old 10-28-2004
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
  
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
code = 'BEGIN{FS=","}
{
printf ("%-11s,%s%s%s,%07.2f,%14s,%-3s\n",$1,substr($2,9,2),substr($2,6,2),substr($
2,3,2),$9,$10,$12)
}


Right, firstly, the Field Separator is set to "," which means that the code is expecting a CSV input file (Comma separated values).

Then, the data from each record is outputted, formatted by printf.

You can see that each formatting character is preceeded by a % symbol. %-11s means print a string left justified in an 11 character wide field. The %s%s%s means print three strings. %07.2f means a 7 digit wide field to two decimal places (floating point number). %14s means 14 char wide right-justified field, etc. The \n is a newline. Then, all the various fields are substituted in place of the %s, etc.

A simple example, printf( "%s-%s\n", $1, $2 ) would cause the first field, a hyphen, and then the second field to be output followed by a newline.

substr( string, start, numchars ) - e.g. substr($2,9,2), this'll return 2 characters starting from the 9th character of the second field of the record.

If you have the manual page on your system (man awk, but man gawk is better), it'll probably explain that lot clearer than I have!

You should probably check this out http://www.gnu.org/software/gawk/manual/gawk.html if you're using GNU awk.

Cheers
ZB
  #3 (permalink)  
Old 10-28-2004
hcclnoodles hcclnoodles is offline
Registered User
  
 

Join Date: Mar 2002
Posts: 272
thankyou
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 10:17 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
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