简体   繁体   English

bash 将行转换为表格格式的列

[英]bash convert rows into columns in table format

I'm trying to convert rows into columns in table format.我正在尝试将行转换为表格格式的列。

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

I used the following awk script to transpose rows into columns我使用以下 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

The above o/p doesn't have proper begin time & End time,Please let me know how to get the formatting right so the time is printed appropriately.上面的 o/p 没有正确的开始时间和结束时间,请告诉我如何正确格式化,以便正确打印时间。

See this script:请参阅此脚本:

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}' 20211222.txt | \
   paste - - - - | \
   column -t -s "$(printf "\t")"

Output: Output:

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

Explanation: In awk, the -F option means field-separator.说明:在 awk 中, -F选项表示字段分隔符。 In your code you used a colon to separate columns from one another.在您的代码中,您使用冒号将列彼此分隔。 However in your input, some lines have more than 1 colon (ie your timestamp field alone has 3 colons) therefore awk interprets these as having 5 columns.但是,在您的输入中,某些行有超过 1 个冒号(即仅您的时间戳字段就有 3 个冒号),因此 awk 将这些解释为有 5 列。

The solution is to add a whitespace to your field separator ( ": " ), since your input does have a whitespace after the first colon and before your second column.解决方案是在字段分隔符 ( ": " ) 中添加一个空格,因为您的输入在第一个冒号之后和第二列之前确实有一个空格。

The usual way to do this is:通常的做法是:

$ cat tst.awk
BEGIN { OFS="\t" }
{
    tag = val = $0
    sub(/[[:space:]]*:.*/,"",tag)
    sub(/[^:]+:[[:space:]]*/,"",val)
    if ( !(tag in tag2val) ) {
        tags[++numTags] = tag
    }
    tag2val[tag] = val
}
!NF { prt(); tag="" }
END { if (tag!="") prt() }

function prt() {
    if ( !doneHdr++ ) {
        for (tagNr=1; tagNr<=numTags; tagNr++ ) {
            tag = tags[tagNr]
            printf "%s%s", tag, (tagNr<numTags ? OFS : ORS)
        }
    }
    for (tagNr=1; tagNr<=numTags; tagNr++ ) {
        tag = tags[tagNr]
        val = tag2val[tag]
        printf "%s%s", val, (tagNr<numTags ? OFS : ORS)
    }
}

$ 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM