我尝试执行以下命令:
ls -ltr | awk 'BEGIN { print "File\t\t\tOwner"} { print $9,"\t",$3} END {print "-DONE \n"}'
Run Code Online (Sandbox Code Playgroud)
并得到以下o/p
File Owner
inventory-shipped root
BBB_list root
marks root
test_file root
awkvars.out root
1 root
t12 root
-DONE
Run Code Online (Sandbox Code Playgroud)
我怎样才能得到o/p喜欢
File Owner
inventory-shipped root
BBB_list root
marks root
test_file root
awkvars.out root
1 root
t12 root
-DONE
Run Code Online (Sandbox Code Playgroud)
除了最后一列之外,您需要使用填充。由于您只有 2 列,因此第一列就足够了。
当您使用printf代替 时print,您可以打印基于模板的所有变量。以下示例打印一个字符串 ( ),并在右侧 ( ) 上%s填充 ( ),因此它变为。24-24%-24s
ls -ltr | awk 'BEGIN {printf "%-24s%s","File","Owner"} { printf "%-24s%s\n",$9,$3} END {print "-DONE \n"}'
Run Code Online (Sandbox Code Playgroud)