Computing.Net > Forums > Unix > Printf in AWK

Printf in AWK

Reply to Message Icon

Original Message
Name: Prasad Chitimalla
Date: January 17, 2003 at 01:51:46 Pacific
Subject: Printf in AWK
OS: Sun OS 5.8
CPU/Ram: sun4u sparc SUNW, sun-fir
Comment:

We have the following printf statement in our AWK program. All the arguments printed are considered as strins even if they are numbers. We have found out that after printing this statement awk runs into following errors

awk: string too long near line 5
awk: syntax error near line 5
awk: illegal statement near line 5
awk: string too long near line 5

Note: line 5 is the below printf statement in our program. all the formatted strings are quite large enough to handle values coming into them.

------------
printf("%5s\t %3s\t %9s\t %12s\t %9s\t %12s\t %9s\t %12s\t %9s\t %12s\t %9s\t %12s\t %9s\t %12s\t %9s\t %12s\t %9s\t %12s\t %9s\t %12s\t %9s\t %12s\t %9s\t %12s\t %12s\t %12s\t %9s\t %8s\t %2s\t %2s\t %2s\n",user,country_code,acct_team_t,acct_team_rec,acct_dir_t,acct_dir_rec,quote_t,quote_rec,quote_lines_t,quote_lines_rec,cobra_quote_t,cobra_quote_rec,m_quote_lines_t,m_quote_lines_rec,cobra_quote_451_t,cobra_quote_451_rec,oppor_300_t,oppor_300_rec,oppor_310_t,oppor_310_rec,oppor_320_t,oppor_320_rec,text_t,ex711_rec,ex761_rec,ex766_rec,tot,date,hr,mm,ss);


Do you see any problems with the number of arguments in the printf statement ?

We have found a solution, by splitting them up into two printf statements and printing it.

Please suggest us on the above problem of printing using a single printf statement.

Thanks & regards
prasad


Report Offensive Message For Removal

Response Number 1
Name: Jimbo
Date: January 17, 2003 at 03:27:48 Pacific
Subject: Printf in AWK
Reply: (edit)

Wow!

That's a nice one.
Can you show the data this is working on?
There must be an easier way than this.

-jim


Report Offensive Follow Up For Removal

Response Number 2
Name: deeps
Date: January 20, 2003 at 06:05:16 Pacific
Subject: Printf in AWK
Reply: (edit)

the data is of the form:
US/archive/00053/20021202.072040.lex.log
v.5.1 US 00053 1 secs: Extracting Account Teams...
v.5.1 US 00053 2 secs: Exported 1 records.
v.5.1 US 00053 2 secs: Extracting Account Directory (031)...
v.5.1 US 00053 2 secs: Exported 24 records.
v.5.1 US 00053 3 secs: Extracting Quotes...
v.5.1 US 00053 3 secs: Exported 3 records.
v.5.1 US 00053 3 secs: Extracting Quote Lines...
v.5.1 US 00053 5 secs: Exported 92 records.
v.5.1 US 00053 5 secs: Extracting Cobra Quote Data (450)...
v.5.1 US 00053 9 secs: Exported 3 records.
v.5.1 US 00053 9 secs: Extracting More Quote Lines ...
v.5.1 US 00053 11 secs: Exported 53 records.
v.5.1 US 00053 11 secs: Extracting Cobra Quote Data (451)...
v.5.1 US 00053 11 secs: Exported 3 records.
v.5.1 US 00053 11 secs: Extracting Opportunities...
v.5.1 US 00053 26 secs: Exported 2 records.
v.5.1 US 00053 26 secs: Extracting Opportunities (310)...
v.5.1 US 00053 42 secs: Exported 0 records.
v.5.1 US 00053 42 secs: Extracting Opportunities (320)...
v.5.1 US 00053 57 secs: Exported 0 records.
v.5.1 US 00053 57 secs: Extracting TextMaster Tables...
v.5.1 US 00053 57 secs: Extracting 711...
v.5.1 US 00053 57 secs: Exported 63 records.
v.5.1 US 00053 57 secs: Extracting 761...
v.5.1 US 00053 58 secs: Exported 46 records.
v.5.1 US 00053 58 secs: Extracting 766...
v.5.1 US 00053 59 secs: Exported 42 records.
v.5.1 US 00053 62 secs: Laptop Extraction completed successfully.

this is only a part of the data, the same sequence goes on repeating henceforth. The above file too is been preapred after greping certain strings as shown above from a different file.

What is done in the awk script is that we try to capture values like the time beside and the number of records exported.

All this is done and print on a single line as we want to import this data onto a excel sheet in Windows. So, instead of using two printf statements we want to find out if there are some limitations as we have seen in our case for printing data in one printf statement.

Thanks.


Report Offensive Follow Up For Removal

Response Number 3
Name: James Boothe
Date: January 23, 2003 at 17:42:46 Pacific
Subject: Printf in AWK
Reply: (edit)

You can have several printf commands that result in just a single output line. All but the last one will not specify a newline character. In fact, when you have such a large number of fields, it makes for easier coding to break it up into multiple printf commands:

awk 'BEGIN {\
printf "This is"
printf " just"
printf " one line\n"
exit
}'


Report Offensive Follow Up For Removal

Response Number 4
Name: deeps
Date: January 24, 2003 at 05:18:31 Pacific
Subject: Printf in AWK
Reply: (edit)

I fully agree with you, James on this issue, that one should spilt printfs and work. I ahve done the same thing above and find out the solution to my problem, but the code which i was trying to modify was already in place by written by someone, and i happen to find out that there is a limitation related to number of characters printed in an AWK statement.

Try a simple example

awk '
{ printf("AAAAAA......... 320 times") }'

and you will see that awk doesnt print all of them.

if you try only a few such "AAAAA then it shall print.

Thanks.

DEEPS


Report Offensive Follow Up For Removal

Response Number 5
Name: James Boothe
Date: January 24, 2003 at 07:52:32 Pacific
Subject: Printf in AWK
Reply: (edit)

On my HP-UX, the maximum length of a printf statement (single or multiple tokens) is 3000 characters:

awk: Format item %s cannot be longer than 3,000 bytes.

And a print command in awk (not printf) went well beyond 3000, and I did not test the maximum for this.

Your limitation of less than 320 seems quite restrictive. I do not know if that limit is fixed for your awk, or maybe configurable. I don't think it relates to your ulimits.

Anyway, sounds like you have a solution.


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Printf in AWK

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software