【发布时间】:2022-01-22 14:28:12
【问题描述】:
我正在尝试将行转换为表格格式的列。
Server Name : dev1-151vd
Status : DONE
Begin time : 2021-12-20 04:30:05.458719-05:00
End time : 2021-12-20 04:33:15.549731-05:00
Server Name : dev2-152vd
Status : DONE
Begin time : 2021-12-20 04:30:05.405746-05:00
End time : 2021-12-20 04:30:45.212935-05:00
我使用以下 awk 脚本将行转换为列
awk -F":" -v n=4 \
'BEGIN { x=1; c=0;}
++c <= n && x == 1 {print $1; buf = buf $2 "\n";
if(c == n) {x = 2; printf buf} next;}
!/./{c=0;next}
c <=n {printf "%4s\n", $2}' temp1.txt | \
paste - - - - | \
column -t -s "$(printf "\t")"
Server Name Status Begin time End time
dev1-151vd DONE 2021-12-20 04 2021-12-20 04
dev2-152vd DONE 2021-12-20 04 2021-12-20 04
以上 o/p 没有正确的开始时间和结束时间,请告诉我如何正确设置格式以便正确打印时间。
【问题讨论】:
-
永远不要对任何输入使用
printf buf,因为当您的输入包含像%s这样的printf 格式化字符时,它会神秘地失败,请始终使用printf "%s", buf。