Tags

, , , ,

If you use print in awk you’ll lose the formatting of the input file — most of the times that may be OK, but not always. The solution is simple — replace print by printf. Here’s an example with strings in the input fields:

$ awk '{printf "%-20s%-20s%-20s\n", $1, $2, $3}' yourfile.dat

.

  • “%…s” means a character string.
  • 20 means a length of 20 characters.
  • awk defaults to right-alignment (presumably for columns of figures) so you need -20 for left-alignment.

Credit: here.

Advertisements