[SATLUG] awk - do I have to manually insert the field delimiter when printing?

Bruce Dubbs bruce.dubbs at gmail.com
Wed Apr 24 14:25:54 CDT 2013


Don Davis wrote:
> Quick question:
>
> When rearranging columns with awk (where the delimiter is a comma):
>
> Can I have awk automatically insert the delimiter ( a comma) using a
> format such as this:
>
> cat somefile |awk -F',' '{print $1 $2 $7 $8 $9 $ 10 $11 $3 $4 $5 $6}'
>
> or do I have to manually specify it:
>
> awk -F',' '{print $1 "," $2 "," $7 "," $8 "," $9 ","  $10 "," $11 "," $3
> "," $4 "," $5 "," $6}'

The -F option (--field-separator) is onlt for input, but the output of

print expr-list

is separated by the value of the OFS variable:

$ echo a b c |awk 'BEGIN{OFS=", "} {print $1, $3, $2 }
a, c, b

   -- Bruce


More information about the SATLUG mailing list