![]() |
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 |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
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 |
|
|||||
|
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 |
|
||||
|
thankyou
|
| Sponsored Links | ||
|
|