gawk: Output Separators

1 
1 5.3 Output Separators
1 =====================
1 
1 As mentioned previously, a 'print' statement contains a list of items
1 separated by commas.  In the output, the items are normally separated by
1 single spaces.  However, this doesn't need to be the case; a single
1 space is simply the default.  Any string of characters may be used as
1 the "output field separator" by setting the predefined variable 'OFS'.
1 The initial value of this variable is the string '" "' (i.e., a single
1 space).
1 
1    The output from an entire 'print' statement is called an "output
1 record".  Each 'print' statement outputs one output record, and then
1 outputs a string called the "output record separator" (or 'ORS').  The
1 initial value of 'ORS' is the string '"\n"' (i.e., a newline character).
1 Thus, each 'print' statement normally makes a separate line.
1 
1    In order to change how output fields and records are separated,
1 assign new values to the variables 'OFS' and 'ORS'.  The usual place to
1 do this is in the 'BEGIN' rule (⇒BEGIN/END), so that it happens
1 before any input is processed.  It can also be done with assignments on
1 the command line, before the names of the input files, or using the '-v'
1 command-line option (⇒Options).  The following example prints the
1 first and second fields of each input record, separated by a semicolon,
1 with a blank line added after each newline:
1 
1      $ awk 'BEGIN { OFS = ";"; ORS = "\n\n" }
1      >            { print $1, $2 }' mail-list
1      -| Amelia;555-5553
1      -|
1      -| Anthony;555-3412
1      -|
1      -| Becky;555-7685
1      -|
1      -| Bill;555-1675
1      -|
1      -| Broderick;555-0542
1      -|
1      -| Camilla;555-2912
1      -|
1      -| Fabius;555-1234
1      -|
1      -| Julie;555-6699
1      -|
1      -| Martin;555-6480
1      -|
1      -| Samuel;555-3430
1      -|
1      -| Jean-Paul;555-2127
1      -|
1 
1    If the value of 'ORS' does not contain a newline, the program's
1 output runs together on a single line.
1