linux - 表格式 Shell 脚本

标签 linux shell scripting

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

我有上面的文件,我想以如下表格格式编写该文件: 时间|事件编号|类型|结果|

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

如何使用 Linux shell 脚本执行此操作?

最佳答案

您可以使用这个 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|

关于linux - 表格式 Shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21674257/

相关文章:

c - 如何设置以 root 身份使用 execl 以用户身份启动应用程序?

c - 套接字 TCP : Why do I lose messages with scanf

linux - bash 内置命令 "select"无法通过 shell 脚本中的管道工作

shell - 当我从oozie使用shell时没有得到这样的文件或目录错误

bash - 删除第 4 列中包含范围模式的行

安卓 : Create a file in/data/local/tmp from an automated test

Linux Bash - 比较 if 语句中的两个变量

bash - 使用 AWK 并将输出捕获到 Bash 中的变量

html - 检查 file.html 是否存在的脚本,如果存在,则运行另一个脚本,否则会死吗?

linux - 文本文件中的单引号完整路径文件无法执行 for 循环处理 mv : cannot stat . .. 没有这样的文件或目录