next up previous 405
Next: Dealing with Files
Up: String Processing
Previous: String substitution


Formatted Printing

A script may process and analyse many datasets, and the results from its calculations will often need presentation, often in tabular form or some aligned output, either for human readability or to be read by some other software.

The UNIX command printf permits formatted output. It is analogous to the C function of the same name. The syntax is

     printf "<format string>" <space-separated argument list of variables>

The format string may contain text, conversion codes, and interpreted sequences.

The conversion codes appear in the same order as the arguments they correspond to. A conversion code has the form

     %[flag][width][.][precision]code

where the items in brackets are optional.

  • The code determines how the output is is converted for printing. The most commonly used appear in the upper table.
  • The width is a positive integer giving the minimum field width. A value requiring more characters than the width is still written in full. A datum needing few characters than the width is right justified, unless the flag is -. * substitutes the next variable in the argument list, allowing the width to be programmable.
  • The precision specifies the number of decimal places for floating point; for strings it sets the maximum number of characters to print. Again * substitutes the next variable in the argument list, whose value should be a positive integer.
  • The flag provides additional format control. The main functions are listed in the lower table.

The interpreted sequences include:
\n for a new line, \" for a double quote, \% for a percentage sign, and \\ for a backslash.



Format codes
Code Interpretation

 
c single character
s string
   
d, i signed integer
o integer written as unsigned octal
x, X integer written as unsigned hexadecimal, the latter using uppercase notation
   
e, E floating point in exponent form m.nnnnne$\pm$xx or m.nnnnnE$\pm$xx respectively
f floating point in mmm.nnnn format
g uses whichever format of d, e, or f is shortest
G uses whichever format of d, E, or f is shortest

 




Flags
Code Purpose

 
- left justify
+ begin a signed number with a + or -
blank Add a space before a signed number that does not begin with a + or -
0 pad a number on the left with zeroes
# use a decimal point for the floating-point conversions, and do not remove trailing zeroes for g and G codes

 


If that's computer gobbledygook here are some examples to make it clearer. The result of follows each printf command, unless it is assigned to a variable through the set mechanism. The commentary after the # is neither part of the output nor should it be entered to replicate the examples. Let us start with some integer values.

     set int = 1234
     set nint = -999
     printf "%8i\n" $int            # 8-character field, right justified
         1234
     printf "%-8d%d\n" $int $nint   # Two integers, the first left justified
     1234    -999
     printf "%+8i\n" $int
        +1234
     printf "%08i\n" $int
     00001234
Now we turn to some floating-point examples.
     set c = 299972.458
     printf "%f %g %e\n" $c $c $c           # The three main codes
     299972.458000 299972 2.999725e+05
     printf "%.2f %.2g %.2e\n" $c $c $c     # As before but set a precision
     299972.46 3e+05 +3.00e+05
     printf "%12.2f %.2G %+.2E\n" $c $c $c  # Show the effect of some flags,
        299972.46 3.0E+05 +3.00E+05         # a width, and E and G codes
Finally we have some character examples of printf.
     set system = Wavelength
     set confid = 95
     set ndf = m31
     set acol = 12
     set dp = 2

     printf "Confidence limit %d%% in the %s system\n" $confid $system
     Confidence limit 95% in the Wavelength system  # Simple case, percentage sign
     printf "Confidence limit %f.1%% in the %.4s system\n" $confid $system
     Confidence limit 95.0% in the Wave system      # Truncates to four characters
     set heading = `printf "%10s: %s\n%10s: %s\n\n" "system" $system "file" $ndf`
     echo $heading                                  # Aligned output, saved to a
         system: Wavelength                         # variable
           file: m31

     printf "%*s: %s\n%*s: %.*f\n\n" $acol "system" \
            $system $acol "confidence" 2 $confid
           system: Wavelength                       # Aligned output with a variable
       confidence: 95.00                            # width and precision

     set heading = ""                               # Form a heading by appending to
     foreach k ( $keywords )                        # variable in a loop.  Note the
        set heading = $heading `printf "%8s  " $k`  # absence of \n.
     end
     echo `printf "%s\n" $heading
   

Note that there are different implementations. While you can check your system's man pages that the desired feature is present, a better way is to experiment on the command line.



next up previous 405
Next: Dealing with Files
Up: String Processing
Previous: String substitution

C-shell Cookbook
Starlink Cookbook 4
Malcolm J. Currie
2006 November 26
E-mail:starlink@jiscmail.ac.uk

Copyright © 2013 Science and Technology Facilities Council