bash 将行转换为 table 格式的列
bash convert rows into columns in table format
我正在尝试将行转换为 table 格式的列。
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 ; buf = buf "\n";
if(c == n) {x = 2; printf buf} next;}
!/./{c=0;next}
c <=n {printf "%4s\n", }' 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 没有正确的开始时间和结束时间,请告诉我如何正确设置格式以便正确打印时间。
查看此脚本:
awk -F": " -v n=4 \
'BEGIN { x=1; c=0;}
++c <= n && x == 1 {print ; buf = buf "\n";
if(c == n) {x = 2; printf buf} next;}
!/./{c=0;next}
c <=n {printf "%4s\n", }' 20211222.txt | \
paste - - - - | \
column -t -s "$(printf "\t")"
输出:
Server Name Status Begin time End time
dev1-151vd DONE 2021-12-20 04:30:05.458719-05:00 2021-12-20 04:33:15.549731-05:00
dev2-152vd DONE 2021-12-20 04:30:05.405746-05:00 2021-12-20 04:30:45.212935-05:00
说明:
在 awk 中,-F
选项表示字段分隔符。在您的代码中,您使用冒号将列彼此分开。但是在您的输入中,有些行有超过 1 个冒号(即您的时间戳字段单独有 3 个冒号)因此 awk 将这些解释为有 5 列。
解决方案是在您的字段分隔符 (": "
) 中添加一个空格,因为您的输入在第一个冒号之后和第二列之前确实有一个空格。
$ cat tst.awk
BEGIN { OFS="\t" }
NF {
if ( ++fldNr == 1 ) {
recNr++
rec = ""
}
tag = val = [=10=]
sub(/[[:space:]]*:.*/,"",tag)
sub(/[^:]+:[[:space:]]*/,"",val)
hdr = hdr (fldNr>1 ? OFS : "") tag
rec = rec (fldNr>1 ? OFS : "") val
next
}
{
if ( recNr == 1 ) {
print hdr
}
print rec
fldNr = 0
}
END { if (fldNr) print rec }
$ awk -f tst.awk file | column -s$'\t' -t
Server Name Status Begin time End time
dev1-151vd DONE 2021-12-20 04:30:05.458719-05:00 2021-12-20 04:33:15.549731-05:00
dev2-152vd DONE 2021-12-20 04:30:05.405746-05:00 2021-12-20 04:30:45.212935-05:00
无论您输入的每条记录有多少行,无论您是否有其他 :
或 %s
或其他任何内容,以上内容都有效。
我正在尝试将行转换为 table 格式的列。
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 ; buf = buf "\n";
if(c == n) {x = 2; printf buf} next;}
!/./{c=0;next}
c <=n {printf "%4s\n", }' 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 没有正确的开始时间和结束时间,请告诉我如何正确设置格式以便正确打印时间。
查看此脚本:
awk -F": " -v n=4 \
'BEGIN { x=1; c=0;}
++c <= n && x == 1 {print ; buf = buf "\n";
if(c == n) {x = 2; printf buf} next;}
!/./{c=0;next}
c <=n {printf "%4s\n", }' 20211222.txt | \
paste - - - - | \
column -t -s "$(printf "\t")"
输出:
Server Name Status Begin time End time
dev1-151vd DONE 2021-12-20 04:30:05.458719-05:00 2021-12-20 04:33:15.549731-05:00
dev2-152vd DONE 2021-12-20 04:30:05.405746-05:00 2021-12-20 04:30:45.212935-05:00
说明:
在 awk 中,-F
选项表示字段分隔符。在您的代码中,您使用冒号将列彼此分开。但是在您的输入中,有些行有超过 1 个冒号(即您的时间戳字段单独有 3 个冒号)因此 awk 将这些解释为有 5 列。
解决方案是在您的字段分隔符 (": "
) 中添加一个空格,因为您的输入在第一个冒号之后和第二列之前确实有一个空格。
$ cat tst.awk
BEGIN { OFS="\t" }
NF {
if ( ++fldNr == 1 ) {
recNr++
rec = ""
}
tag = val = [=10=]
sub(/[[:space:]]*:.*/,"",tag)
sub(/[^:]+:[[:space:]]*/,"",val)
hdr = hdr (fldNr>1 ? OFS : "") tag
rec = rec (fldNr>1 ? OFS : "") val
next
}
{
if ( recNr == 1 ) {
print hdr
}
print rec
fldNr = 0
}
END { if (fldNr) print rec }
$ awk -f tst.awk file | column -s$'\t' -t
Server Name Status Begin time End time
dev1-151vd DONE 2021-12-20 04:30:05.458719-05:00 2021-12-20 04:33:15.549731-05:00
dev2-152vd DONE 2021-12-20 04:30:05.405746-05:00 2021-12-20 04:30:45.212935-05:00
无论您输入的每条记录有多少行,无论您是否有其他 :
或 %s
或其他任何内容,以上内容都有效。