printf: placing a percent sign 
Author Message
 printf: placing a percent sign

Hi!

        I already solved this little problem of mine with regards to having
zero as my denominator when I'm dividing numbers(the reason why I change
the subject).  One last question with regards to this posting.  

My last question is on how to insert a percentage sign after a floating
point number when using  "printf".

        My script contains this:

printf "%1s %5s $%5s %7.2f %5s\n",$1, $2, $3, calc, $5

        I tried doing the following but still I can't show the percentage using
printf.

printf "%1s %5s $%5s %7.2f% %5s\n",$1, $2, $3, calc, $5  
    0.39%5s

printf "%1s %5s $%5s %7.2f"%" %5s\n",$1, $2, $3, calc, $5
    awk: division by zero in mod
    input record number 1, file 41008.sec
    source line number 9

printf "%1s %5s $%5s %7.2f\% %5s\n",$1, $2, $3, calc, $5
    0.39\%5s

printf "%1s %5s $%5s %7.2f"\%" %5s\n",$1, $2, $3, calc, $5
    awk: syntax error at source line 9
         context is .....................................
    .....................................................
    awk: illegal statement at source line 9

printf "%1s %5s $%5s %7.2f\"%" %5s\n",$1, $2, $3, calc, $5
    awk: syntax error at source line 9
         context is  .....................................
         .................................................
    awk: illegal statement at source line 9

        The only way I can make the percent (%) appear at the end of the
floating
point number is by using:
awk '
{
print $1"\t", $2"\t", $3"\t", calc"%""\t", $5"\t"}' infile > outfile

        Does anybody knows how to place a percent sign (%) after a floating
point number using printf?
What other escape character did I miss?
        Any suggestions on how to do this?   Just send me an email or
you can answer me by posting in
this group.

        Thank you very much.

Albert

**********************************************************************************

Quote:


> >Hi guys,

> >        I'm really new to awk and I need to do some "simple"arithmetic
> >calculations using awk.
> >I know this is a very easy calculation but just can't figure it out on
> >my own.  I need some help.
> >Whenever my "infile" contains a Zero (0) value in one of its field and
> >it becomes a denominator
> >in my simple arithmetic calculation, I always got this message:

> >1        2       $3      awk: division by zero
> >input record number 1, file 1.1
> Correct - that's what most programming language will do ;-)

> What you want to do is put the awk program in a file (to make it
> easier to manage/control) and build in some specific code to check for it
> The file prog.awk will contain
> >awk '
> {
>   calc=0
>   if ($4+0 > 0) calc= ((($3-(($2+$6)/$4))+$1)/2)*100
>   print $1"\t", $2"\t", "$"$3"\t", "\t", calc,"\t", $5"\t"}

> And then type
> awk -f prog.awk infile     ....

> As regards formatting, you may find it easier to use the printf statement
> heres an example

>   printf "%10s %10s $%-10s %7.2f %10s\n",$1, $2, $3, calc, $5

> Also you may want to use the INT function to avoid rounding errors
> Hope this helps
> Mark
> ps There may be syntax errors - please check
> --
> Mark Katz
> ISPC, London - Innovation in data-delivery tools
> Tel: (44) 181-455 4665, Fax (44) 181-458 9554
> ** See our website at http://www.*-*-*.com/ **

--
Just me,
Albert


Fri, 06 Apr 2001 03:00:00 GMT  
 printf: placing a percent sign

Quote:

> Hi!

>         I already solved this little problem of mine with regards to having
> zero as my denominator when I'm dividing numbers(the reason why I change
> the subject).  One last question with regards to this posting.

> My last question is on how to insert a percentage sign after a floating
> point number when using  "printf".

>         My script contains this:

> printf "%1s %5s $%5s %7.2f %5s\n",$1, $2, $3, calc, $5

>         I tried doing the following but still I can't show the percentage using
> printf.

> printf "%1s %5s $%5s %7.2f% %5s\n",$1, $2, $3, calc, $5
>     0.39%5s

> printf "%1s %5s $%5s %7.2f"%" %5s\n",$1, $2, $3, calc, $5
>     awk: division by zero in mod
>     input record number 1, file 41008.sec
>     source line number 9

> printf "%1s %5s $%5s %7.2f\% %5s\n",$1, $2, $3, calc, $5
>     0.39\%5s

> printf "%1s %5s $%5s %7.2f"\%" %5s\n",$1, $2, $3, calc, $5
>     awk: syntax error at source line 9
>          context is .....................................
>     .....................................................
>     awk: illegal statement at source line 9

> printf "%1s %5s $%5s %7.2f\"%" %5s\n",$1, $2, $3, calc, $5
>     awk: syntax error at source line 9
>          context is  .....................................
>          .................................................
>     awk: illegal statement at source line 9

>         The only way I can make the percent (%) appear at the end of the
> floating
> point number is by using:
> awk '
> {
> print $1"\t", $2"\t", $3"\t", calc"%""\t", $5"\t"}' infile > outfile

>         Does anybody knows how to place a percent sign (%) after a floating
> point number using printf?
> What other escape character did I miss?
>         Any suggestions on how to do this?   Just send me an email or
> you can answer me by posting in
> this group.

>         Thank you very much.

> Albert

Just put the percent sign as an ouput parameter

printf "%1s %5s $%5s %7.2f%s %5s\n",$1, $2, $3, calc, "%", $5

Cesar
--
Please remove the uppercase characters from my e-mail address for the
real thing



Fri, 06 Apr 2001 03:00:00 GMT  
 printf: placing a percent sign


Quote:
>Hi!

>I already solved this little problem of mine with regards to having
>zero as my denominator when I'm dividing numbers(the reason why I change
>the subject).  One last question with regards to this posting.  

>My last question is on how to insert a percentage sign after a floating
>point number when using  "printf".

>    My script contains this:

>printf "%1s %5s $%5s %7.2f %5s\n",$1, $2, $3, calc, $5

>    I tried doing the following but still I can't show the percentage using
>printf.

>printf "%1s %5s $%5s %7.2f% %5s\n",$1, $2, $3, calc, $5  
>    0.39%5s

To put a % in printf, use %%, as mentioned in the man page.  RTFM

printf "%1s %5s $%5s %7.2f%% %5s\n",$1, $2, $3, calc, $5
                          ^^

should do what you want.

Chuck Demas
Needham, Mass.

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

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



Fri, 06 Apr 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Percent and Equal Signs

2. Percent sign in format string?

3. Percent indication in place

4. Good Sign or Bad Sign?

5. place control in edit in place

6. Grand Total detail percent problem

7. CW2 Report percent grand total problem

8. Percent character in message catalog file?

9. Percent of the grand total

10. Product Scope 32 PRO - Limited Time 50 Percent Sale

11. PROPPRINT:Percent

12. Landmark Study Reveals 35 Percent Productivity Gains for Businesses Using Model-Driven Architecture

 

 
Powered by phpBB® Forum Software