简体   繁体   中英

Table Format Shell Script

event
time:00.00
event no.:01
Type:A
result:success

event
time:00.01
event no.:02
result:success 

event
time:00.02
event no.:03
Type:B
result:fail

I have the above file and I want to write this file in table format like this: time|event no.|Type|result|

00.00|01|A|success|
00.01|02||success|
00.02|03|B|fail|

How to do this using linux shell scripting?

You can use this awk:

awk -F' *: *' 'NF>1{a[$1]=$2; next}
  /^event/ && NR>5{print a["time"], a["event no."], a["Type"], a["result"] OFS;delete a;next}
    END{print a["time"], a["event no."], a["Type"], a["result"] OFS}' OFS='|' file

00.00|01|A|success|
00.01|02||success|
00.02|03|B|fail|

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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