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




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #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