1

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

也就是说,我从中得到了结果,标题为 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 文件中,以便它最终成为我发布的示例?

谢谢!

4

2 回答 2

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 ]不断添加第三列的内容。

然后,当您循环浏览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块中打印它。

于 2015-08-11T11:32:21.947 回答
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
于 2015-08-11T11:48:45.677 回答