如何合并两个awk结果?

时间:2015-08-11 11:26:09

标签: bash csv awk pattern-matching sar

我正在尝试绘制一些趋势图,因此我希望“简化”我在电子表格中执行的手动数据收集过程并使其轻松运行。

也就是说,我得到了这个结果,标题为Timestamp,Core#,100 - %Idle:

# sadf -d -- -P ALL -s 00:00:00 -e 01:30:00 | awk '{FS=";"} {if ($4 != -1) {print $3","$4","100 - $NF}}' | sed '1d' &>cpuresults.txt

2015-08-11 05:10:04 UTC,0,22.87
2015-08-11 05:10:04 UTC,1,15.99
2015-08-11 05:10:04 UTC,2,9.81
2015-08-11 05:10:04 UTC,3,7.95
2015-08-11 05:20:03 UTC,0,19.38
2015-08-11 05:20:03 UTC,1,15.63
2015-08-11 05:20:03 UTC,2,9.3
2015-08-11 05:20:03 UTC,3,6.59
2015-08-11 05:30:06 UTC,0,18.42
2015-08-11 05:30:06 UTC,1,16.7
2015-08-11 05:30:06 UTC,2,9.06
2015-08-11 05:30:06 UTC,3,6.22
2015-08-11 05:40:03 UTC,0,19.93
2015-08-11 05:40:03 UTC,1,16.27
2015-08-11 05:40:03 UTC,2,9.41
2015-08-11 05:40:03 UTC,3,7.8
2015-08-11 05:50:03 UTC,0,20.84
2015-08-11 05:50:03 UTC,1,15.86
2015-08-11 05:50:03 UTC,2,9.16
2015-08-11 05:50:03 UTC,3,8.33
2015-08-11 06:00:07 UTC,0,18.68
2015-08-11 06:00:07 UTC,1,16.93
2015-08-11 06:00:07 UTC,2,9.58
2015-08-11 06:00:07 UTC,3,6.45
2015-08-11 06:10:01 UTC,0,21.76
2015-08-11 06:10:01 UTC,1,15.4
2015-08-11 06:10:01 UTC,2,10.48
2015-08-11 06:10:01 UTC,3,8.65
2015-08-11 06:20:01 UTC,0,16.01
2015-08-11 06:20:01 UTC,1,14.35
2015-08-11 06:20:01 UTC,2,8.12
2015-08-11 06:20:01 UTC,3,5.89

这个,标题为Timestamp,Memory Used:

# sadf -dh -- -r -s 00:00:00 -e 01:30:00 | awk '{FS=";"} {print $3","$6}' | sed '1d' &>memresults.txt

2015-08-11 05:10:04 UTC,52.00
2015-08-11 05:20:03 UTC,51.93
2015-08-11 05:30:06 UTC,52.07
2015-08-11 05:40:03 UTC,52.02
2015-08-11 05:50:03 UTC,51.89
2015-08-11 06:00:07 UTC,52.03
2015-08-11 06:10:01 UTC,51.98
2015-08-11 06:20:01 UTC,51.98

我正试图获得这样的输出:

Timestamp,Memory,Core0,Core1,Core2,Core3
2015-08-11 05:10:04 UTC,52.00,22.87,15.99,9.81,7.95
2015-08-11 05:20:03 UTC,51.93,19.38,15.63,9.3,6.59
2015-08-11 05:30:06 UTC,52.07,18.42,16.7,9.06,6.22
2015-08-11 05:40:03 UTC,52.02,19.93,16.27,9.41,7.8
2015-08-11 05:50:03 UTC,51.89,20.84,15.86,9.16,8.33
2015-08-11 06:00:07 UTC,52.03,18.68,16.93,9.58,6.45
2015-08-11 06:10:01 UTC,51.98,21.76,15.4,10.48,8.65
2015-08-11 06:20:01 UTC,51.98,16.01,14.35,8.12,5.89

所以,正如你所看到的,它只是转置每个CPU内核的最后一行,但我看不出怎么做。此外,可能还有未来的核心。

我尝试在awk上做一个循环(使用cpucount变量作为限制),只打印最大核心数的结果,匹配两个文件的时间戳,但我没有走得太远。

你能帮我解决一下awk公式来转换CPU核心值并将它们添加到memresults.txt文件中,以便它最终作为我发布的例子吗?

谢谢!

2 个答案:

答案 0 :(得分:4)

只需将数据存储在数组中,稍后再打印:

awk -F, 'FNR==NR {data[$1] = (data[$1]?data[$1] FS $3:$3); next}
         {print $0 FS data[$1]}' cpu mem

这会将cpu文件中的所有数据存储到数组data[]中。 data [ first column ]不断添加第3列的内容。

然后,当您循环浏览mem文件时,只需打印整行,然后是来自data [ 1st column ]的对应数据。

$ awk -F, 'FNR==NR {a[$1] = (a[$1]?a[$1] FS $3:$3); next} {print $0 FS a[$1]}' cpu mem
2015-08-11 05:10:04 UTC,52.00,22.87,15.99,9.81,7.95
2015-08-11 05:20:03 UTC,51.93,19.38,15.63,9.3,6.59
2015-08-11 05:30:06 UTC,52.07,18.42,16.7,9.06,6.22
2015-08-11 05:40:03 UTC,52.02,19.93,16.27,9.41,7.8
2015-08-11 05:50:03 UTC,51.89,20.84,15.86,9.16,8.33
2015-08-11 06:00:07 UTC,52.03,18.68,16.93,9.58,6.45
2015-08-11 06:10:01 UTC,51.98,21.76,15.4,10.48,8.65
2015-08-11 06:20:01 UTC,51.98,16.01,14.35,8.12,5.89

要设置标题Timestamp,Memory,Core0,Core1,Core2,Core3,您可以将其打印在BEGIN块中。

答案 1 :(得分:0)

将file1作为

2015-08-11 05:10:04 UTC,0,22.87
2015-08-11 05:10:04 UTC,1,15.99
2015-08-11 05:10:04 UTC,2,9.81
2015-08-11 05:10:04 UTC,3,7.95

和file2为

2015-08-11 05:10:04 UTC,52.00

您可以使用cutpaste,如下所示:

cut -d, -f3 file1 | paste -d, - - - - | paste -d, file2 -

使用sed '1i..'

添加标题行
cut -d, -f3 file1 | paste -d, - - - - | paste -d, file2 - | sed '1iheader text'

结果

header text
2015-08-11 05:10:04 UTC,52.00,22.87,15.99,9.81,7.95