awk syntax question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk syntax question
# 1  
Old 10-28-2004
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  
Old 10-28-2004
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  
Old 10-28-2004
thankyou
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Question regarding quotation syntax

Hey guys, my first post on UNIX Forums(much overdue IMO)! I've got this bit of code that doesn't seem to be working correctly for an Android app I'm working on: "screen -S gmod1 -p 0 -X stuff " & "" & command.text & "`echo -ne '\015'`""" Basically it types command.text(variable determined... (4 Replies)
Discussion started by: stingwraith
4 Replies

2. Programming

Perl syntax question

Hallo everybody, I have a following problem - I'm doing a map funciont to fill in a HTML table and I want to use some radiobutton groups. Unfortunatelly, they are grouped by names, so I have to add some "counter" that will divide one row from another, and I'm using CGI.pm for generating the... (3 Replies)
Discussion started by: duskos
3 Replies

3. Shell Programming and Scripting

Question about syntax error

first of all.. sorry about all the question bombing.. im bored atm so im currently playing around with sh scripting hehe s = `expr ls -s Documents | grep Music | awk '{ print $1 }' ` t = `expr $t + $s` it give syntax error s not found t not found lol... any idea why? (7 Replies)
Discussion started by: Nick1097
7 Replies

4. Shell Programming and Scripting

tar -C syntax question

I am writing a perl script to tar multiple files (in unix) from a given directory to a given output directory. I do NOT want the file path included in the tar, so I've flagged the -C option. Example: tar -cvf tar/1.tar -C htmp/source/ 1-1-1.xml However, I need to do this for a number of target... (3 Replies)
Discussion started by: michanjohns
3 Replies

5. UNIX for Advanced & Expert Users

SNMP syntax question

Hello, I need to create an snmp.comf file that defines 2 IPs to the same community string. Do I need to have 2 community strings with the same name and diff't IPs? Or should I have 1 string and list the IPs? (comma seperated?) Example: rocommunity EC_8000_RO arguments EC_8000_RO... (2 Replies)
Discussion started by: felbvts
2 Replies

6. Shell Programming and Scripting

syntax question in regards to nested awk statements

Hello all, I am writing up an input file and I was hoping I could get some guidance as to how to best consolidate these 2 awk statements for 1 while loop. Here's my input file # cat databases.lst #NOTE: These entries are delimited by tabs "\t" #oracleSID name/pass # db11 ... (2 Replies)
Discussion started by: Keepcase
2 Replies

7. Shell Programming and Scripting

OR operator syntax question in AWK script

Hi guys, I confused about syntax used in OR script as follow: I have this sample file separated by "|" containing: January|Month No. 1 February|Month No. 2 March|Month No. 3 April|Month No. 4 May|Month No. 5 June|Month No. 6 July|Month No. 7 August|Month No. 8 September|Month No. 9... (11 Replies)
Discussion started by: cgkmal
11 Replies

8. Shell Programming and Scripting

awk syntax question

Hi I use awk command to delete the first blanc line of a file: awk '/^$/ && !f{f=1;next}1' infile > outfile can somebody please explain me what the last "1'" in !f{f=1;next}1' stands for... Thansk a lot -A (3 Replies)
Discussion started by: aoussenko
3 Replies

9. UNIX for Dummies Questions & Answers

AWK syntax question

Hi, Have to check file names in some given directory. SO, What is the right syntax here: *$3*=="'$object_list'" - just wanted to check if $3 is in the object_list. And also, Do I need so many quotes around? (5 Replies)
Discussion started by: Leo_NN
5 Replies

10. Shell Programming and Scripting

yet another awk field syntax question

I am trying to print the remaing fields and field numbers beginning with a field 'xyz' #cat abc test1:test2:xyz:test3:test4:test5 #cat def test1:test2:test3:xyz:test4:test5 desired output is to be able to print NF and any trailing fields separated by':' test3 3 or test4 3 or test5... (4 Replies)
Discussion started by: prkfriryce
4 Replies
Login or Register to Ask a Question