printf in awk/nawk 
Author Message
 printf in awk/nawk

I have a question regarding using printf in nawk. I need to control
string length by an nawk variable. I know the following
{printf "%10s", $3}
however, when the length I want to control printing $3 is a nawk
variable, lets say fieldl, I don't know how to do the printing.
Obviously, {printf "%fieldls",$3} doesn't work.
Thanks for the help.

xixi



Mon, 26 Mar 2001 03:00:00 GMT  
 printf in awk/nawk

Quote:

>I have a question regarding using printf in nawk. I need to control
>string length by an nawk variable. I know the following
>{printf "%10s", $3}
>however, when the length I want to control printing $3 is a nawk
>variable, lets say fieldl, I don't know how to do the printing.
>Obviously, {printf "%fieldls",$3} doesn't work.

From the gawk man page (speaking about printf):

|     There are  optional,  additional  parameters  that  may  lie
|     between the % and the control letter:
|
|     -    The expression  should  be  left-justified  within  its
|          field.
|
|     width
|          The field should be padded to this width. If the number
|          has  a leading zero, then the field will be padded with
|          zeros.  Otherwise  it  is  padded  with  blanks.   This
|          applies even to the non-numeric output formats.
|
|     .prec
|          A number indicating the maximum  width  of  strings  or
|          digits to the right of the decimal point.
|
|     The dynamic width  and  prec  capabilities  of  the  ANSI  C
|     printf() routines are supported.  A * in place of either the
|     width or prec specifications will cause their values  to  be
|     taken from the argument list to printf or sprintf().

Thus  printf("%*.*s",padded_width,max_width,$3) seems to be what you
want to use.  I think I've used this once or twice in the past.

I can't tell you if this works for your awk or nawk.

man gawk
man printf

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Mon, 26 Mar 2001 03:00:00 GMT  
 printf in awk/nawk


Quote:


>>I have a question regarding using printf in nawk. I need to control
>>string length by an nawk variable. I know the following
>>{printf "%10s", $3}
>>however, when the length I want to control printing $3 is a nawk
>>variable, lets say fieldl, I don't know how to do the printing.
>>Obviously, {printf "%fieldls",$3} doesn't work.

<snip>
>Thus  printf("%*.*s",padded_width,max_width,$3) seems to be what you
>want to use.  I think I've used this once or twice in the past.

And it it doesnt work, then you can roll your own format string like
this:
        fmt = "%" fieldl "s"        # create printf control string
        printf fmt,$3           # do it

Quote:
>Chuck Demas
>Needham, Mass.

--
Steve


Mon, 26 Mar 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Printf formatting in nawk

2. awk, nawk & gawk

3. help for new user looking to use awk nawk or mawk

4. Limit for the Line handle by NAWK/AWK on SOLARIS 2.5.7

5. dynamic formatting not available in awk/oawk/nawk?

6. AWK/NAWK/GAWK questions !

7. search on variable passed to awk /nawk

8. How is this done in awk/nawk/gawk?

9. Whats nawk ( compare to awk ? )

10. ignoring case in awk/nawk searches

11. Differences between awk/nawk/gawk/mawk

12. How to distinquish AWK vs NAWK

 

 
Powered by phpBB® Forum Software