Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.
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

1 Answer 1

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.