Why is this awk statement not working?

0

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.

cardycakes

Posted 2013-11-08T11:48:17.357

Reputation: 19

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 – 2013-11-08T23:25:29.103

Answers

0

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[@]}"

glenn jackman

Posted 2013-11-08T11:48:17.357

Reputation: 18 546