How to align columns with awk

前端 未结 1 1630
庸人自扰
庸人自扰 2021-01-29 08:36

I tried to execute below command:

ls -ltr | awk \'BEGIN { print \"File\\t\\t\\tOwner\"} { print $9,\"\\t\",$3} END {print \"-DONE \\n\"}\'

and

1条回答
  •  深忆病人
    2021-01-29 09:38

    You need to use padding except for the last column. Since you have only 2 columns, the first will be enough.

    When you use printf instead of print, you can print all the variables based on a template. The following examples print a string (%s) with a padding (24) on the right side (-24), so it becomes %-24s.

    ls -ltr | awk 'BEGIN {printf "%-24s%s","File","Owner"} { printf "%-24s%s\n",$9,$3} END {print "-DONE \n"}'
    

    For more information you can check this or this.

    0 讨论(0)
提交回复
热议问题