An Awk Primer/Output with print and printf

< An Awk Primer

The simplest output statement is the by-now familiar "print" statement. There's not too much to it:

The "printf()" (formatted print) function is much more flexible, and trickier. It has the syntax:

   printf(<string>,<expression list>)

The "string" can be a normal string of characters:

   printf("Hi, there!")

This prints "Hi, there!" to the display, just like "print" would, with one slight difference: the cursor remains at the end of the text, instead of skipping to the next line, as it would with "print". A "newline" code ("\n") has to be added to force "printf()" to skip to the next line:

   printf("Hi, there!\n")

So far, "printf()" looks like a step backward from "print", and for do dumb things like this, it is. However, "printf()" is useful when precise control over the appearance of the output is required. The trick is that the string can contain format or "conversion" codes to control the results of the expressions in the expression list. For example, the following program:

   BEGIN {x = 35; printf("x = %d decimal, %x hex, %o octal.\n",x,x,x)}

-- prints:

   x = 35 decimal, 23 hex, 43 octal.

The format codes in this example include: "%d" (specifying decimal output), "%x" (specifying hexadecimal output), and "%o" (specifying octal output). The "printf()" function substitutes the three variables in the expression list for these format codes on output.

   x = 35;     printf("x = %d\n",x)      # yields:  x = 35    
   x = 3.1415; printf("x = %d\n",x)      # yields:  x = 3    
   x = "TEST"; printf("x = %d\n",x)      # yields:  x = 0
   x = 255; printf("x = %o\n",x)          # yields:  x = 377
   x = 197; printf("x = %x\n",x)          # yields:  x = c5
   BEGIN {for (ch=32; ch<128; ch++) printf("%c   %c\n",ch,ch+128)}
   x = "jive"; printf("string = %s\n",x)  # yields:  string = jive
   [-]D.DDDDDDe[+/-]DDD

For example:

   x = 3.1415; printf("x = %e\n",x)       # yields:  x = 3.141500e+000
   [-]D.DDDDDD

For example:

   x = 3.1415; printf("x = %f\n",x)       # yields:  f = 3.141500
   %3d    %5.2f    %08s    %-8.4s

This works as follows:


For example, consider the output of a string:

   x = "Baryshnikov"
   printf("[%3s]\n",x)     # yields: [Baryshnikov]
   printf("[%16s]\n",x)    # yields: [     Baryshnikov]
   printf("[%-16s]\n",x)   # yields: [Baryshnikov     ]
   printf("[%.3s]\n",x)    # yields: [Bar]
   printf("[%16.3s]\n",x)  # yields: [             Bar]
   printf("[%-16.3s]\n",x) # yields: [Bar             ]
   printf("[%016s]\n",x)   # yields: [00000Baryshnikov]
   printf("[%-016s]\n",x)  # yields: [Baryshnikov     ]

-- or an integer:

   x = 312
   printf("[%2d]\n",x)   # yields: [312]
   printf("[%8d]\n",x)   # yields: [     312]
   printf("[%-8d]\n",x)  # yields: [312     ]
   printf("[%.1d]\n",x)  # yields: [312]
   printf("[%08d]\n",x)  # yields: [00000312]
   printf("[%-08d]\n",x) # yields: [312     ]

-- or a floating-point number:

   x = 251.67309
   printf("[%2f]\n",x)     # yields: [251.67309]
   printf("[%16f]\n",x)    # yields: [      251.67309]
   printf("[%-16f]\n",x)   # yields: [251.67309      ]
   printf("[%.3f]\n",x)    # yields: [251.673]
   printf("[%16.3f]\n",x)  # yields: [        251.673]
   printf("[%016.3f]\n",x) # yields: [00000000251.673]
This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.