我有一个包含对象信息的日志文件。我需要AWK中的脚本
我的日志将是这样的:
ObjectA copy time 2ms
ObjectA ID is 23467sdfs3453AAAA
ObjectB copy time 7ms
Objectc run time 23ms
ObjectA process time 3ms
ObjectA run time 1ms
Objectc copy time 71ms
ObjectB ID is sada67AsdfsdfAA345
ObjectB process time 9ms
Objectc process time 29ms
ObjectB run time 4ms
ObjectC ID is 534267AsdfsdfAA233
...
我的输出应该是
Objectname | Object ID | CopyTime | RunTime | ProcessTime
----------------------------------------------------------------------------
ObjectA | 23467sdfs3453AAAA | 2ms | 1ms | 3ms
----------------------------------------------------------------------------
ObjectB | sada67AsdfsdfAA345 | 7ms | 4ms | 9ms
----------------------------------------------------------------------------
ObjectC | 534267AsdfsdfAA233 | 71ms | 23ms | 29ms
---------------------------------------------------------------------------
完整的要求。 表格格式。
答案 0 :(得分:0)
我会将grep
与tr
一起用作列表的预处理器:
tr ',' '\n' < objects.txt | grep -Ff- logfile
tr
命令将对象文件从逗号分隔文件转换为文件位于单独行的文件:
ObjectA
ObjectB
ObjectC
grep -f-
将此文件用作搜索模式列表。请注意,-
是文件名的特殊值,表示stdin。 -F
将模式视为文字字符串而不是正则表达式。
答案 1 :(得分:0)
以下awk
脚本将解析给定文件,并使用键入输入第一列的关联数组存储每个对象的相关信息。最后,它创建了一个简单的表格。
#!/usr/bin/awk -f
$2 == "copy" {
copy_time[$1] = $NF;
}
$2 == "ID" {
id[$1] = $NF;
}
$2 == "run" {
run_time[$1] = $NF;
}
$2 == "process" {
process_time[$1] = $NF;
}
END {
fmt = "%-15s| %-20s | %-10s | %-10s | %-10s\n";
div = "------------------------------------------------------------------------------";
printf(fmt, "Object Name", "Object ID", "Copy Time",
"Run Time", "Process Time");
print(div);
for (obj in id) {
printf(fmt, obj, id[obj], copy_time[obj],
run_time[obj], process_time[obj]);
print(div);
}
}
运行它(更正大写为ObjectC
):
$ ./script.awk data.in
Object Name | Object ID | Copy Time | Run Time | Process Time
------------------------------------------------------------------------------
ObjectA | 23467sdfs3453AAAA | 2ms | 1ms | 3ms
------------------------------------------------------------------------------
ObjectB | sada67AsdfsdfAA345 | 7ms | 4ms | 9ms
------------------------------------------------------------------------------
ObjectC | 534267AsdfsdfAA233 | 71ms | 23ms | 29ms
------------------------------------------------------------------------------