Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
awk 'BEGIN { COLM_FMT = "%-8s, %-8s, %-8s, %-8s, %-8s, %-8s, %-8s, %-8s, %-8s, %-8s,    %-8s, %-8s, %-8s, %-8s,\n" }
           { printf COLM_FMT, ${totals[0]}, ${totals[1]}, ${totals[2]}, ${totals[3]}, ${totals[4]}, ${totals[5]}, ${totals[6]}, ${totals[7]},
           ${totals[8]}, ${totals[9]}, ${totals[10]}, ${totals[11]}, ${totals[12] }'

Why is the above awk statement giving me the following errors:

'awk: syntax error near line 2' 'awk: illegal statement near line2'

This is within a bash script.

share|improve this question
Is the awk statement as shown, with the printf statement split over two lines (which are lines 2 and 3)? If so, try putting a backslash at the end of line 2 (the first line of the printf). – Scott Nov 8 '13 at 23:25

You're putting bash variables in a awk script but putting the awk body in single quotes which prevents variable expansion.

Are you aware that bash has a printf command? (type help printf at a bash prompt)

fmt="%-8s, %-8s, %-8s, %-8s, %-8s, %-8s, %-8s, %-8s, %-8s, %-8s,    %-8s, %-8s, %-8s, %-8s,\n"
printf "$fmt" "${totals[@]}"
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .