Skip to main content
2 of 2
updated title
enharmonic
  • 742
  • 9
  • 15

Using awk '{printf...' multiple columns

How do I print multiple columns using awk '{printf...? With awk '{print... it is easy:

$ printf bar\\t5 | awk '{print $2,"baz",$1}' 5 baz bar 

But if I want to format the digit to show a certain number of decimals, the other columns disappear:

$ printf bar\\t5 | awk '{printf "%.3f\n", $2,"baz",$1}' 5.000 

I tried putting the printf statement before each column I wanted formatted, but that resulted in a syntax error:

$ printf bar\\t5 | awk '{printf "%.3f\n",$2, printf "%4s\n","baz", $1}' awk: cmd. line:1: {printf "%.3f\n",$2, printf "%s\n","baz", $1} awk: cmd. line:1: ^ syntax error 

or without the printf and just the format string before each column:

$ printf bar\\t5 | awk '{printf "%.3f\n",$2,"%4s\n","baz", $1}' 5.000 

How do I get all the columns to appear when using awk '{printf...?

enharmonic
  • 742
  • 9
  • 15