DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
Using awk

Output separators

The built-in variables OFS and ORS contain the output field separator and record separator respectively. Initially, OFS is set to a single blank and ORS to a single newline, but you can change these values at any time. For example, the following program prints the first and second fields of each record with a colon between the fields and two newlines after the second field:

   BEGIN  { OFS = ":"; ORS = "\n\n" }
          { print $1, $2 }
Run on the countries file, the following output is generated (only the first few lines are shown):
   CIS:8650
   

Canada:3852

China:3692

Notice that the following program prints the first and second fields with no intervening output field separator:
   { print $1 $2 }
This is because $1 $2 is a string consisting of the concatenation of the first two fields. The output is as follows:
   CIS8650
   

Canada3852

You may want to change the value of OFS or ORS if you are reading or writing a file used by an application that does not follow the SCO OpenServer convention of separating fields by tabs and records by newlines.For example, in data files created by the language BASIC, fields are surrounded by double quotes and separated by commas, while records are separated by newlines. Therefore, to create a data file in awk that could be used by a BASIC program, you would need to set OFS to be a comma, and remember to print all output fields surrounded by double quotes.
Next topic: The printf statement
Previous topic: The print statement

© 2005 The SCO Group, Inc. All rights reserved.
SCO OpenServer Release 6.0.0 -- 03 June 2005