如何通过扭曲合并两个 awk 结果?

How to merge two awk results with a twist?

我正在尝试绘制一些趋势图,所以我想“简化”我在电子表格上进行的手动数据收集过程并轻松 运行。

也就是说,我从中得到了结果,headers Timestamp,Core #,100 - %Idle:

# sadf -d -- -P ALL -s 00:00:00 -e 01:30:00 | awk '{FS=";"} {if ( != -1) {print ","","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

而这个,带有 headers 时间戳,使用的内存:

# sadf -dh -- -r -s 00:00:00 -e 01:30:00 | awk '{FS=";"} {print ","}' | 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 文件中,这样它就变成了我发布的示例吗?

谢谢!

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

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

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

然后,当您遍历 mem 文件时,只需打印整行,然后打印来自 data [ 1st column ].

的对应数据
$ awk -F, 'FNR==NR {a[] = (a[]?a[] FS :); next} {print [=11=] FS a[]}' 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

要获得 header Timestamp,Memory,Core0,Core1,Core2,Core3,您可以将其打印在 BEGIN 块中。

文件 1 为

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 -

添加 header 行 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