標準出力には次のデータがあります。
13:46:17 0.8 cpu(1)
13:46:17 0.7 cpu(2)
13:46:17 1.1 cpu(3)
13:46:24 0.6 cpu(1)
13:46:24 0.5 cpu(2)
13:46:24 0.9 cpu(3)
13:46:31 0.8 cpu(3)
13:46:38 0.9 cpu(3)
13:46:52 0.5 cpu(1)
13:46:52 0.8 cpu(3)
13:46:59 0.6 cpu(1)
13:46:59 0.9 cpu(3)
13:47:06 0.6 cpu(2)
13:47:13 0.5 cpu(2)
このスクリプトを使用すると、次のような結果が得られます。
['Time', 'cpu(1)', 'cpu(2)', 'cpu(3)'],
['13:46:17', 0.8, 0.7, 1.1],
['13:46:24', 0.6, 0.5, 0.9],
['13:46:31', , , 0.8],
['13:46:38', , , 0.9],
['13:46:52', 0.5, , 0.8],
['13:46:59', 0.6, , 0.9],
['13:47:06', , 0.6, ],
['13:47:13', , 0.5, ]
以下の例が必要です。13:46:31にCPU(1)値がない場合は、古いCPU(1)値を新しいタイムスタンプにコピーする必要があります。
['Time', 'cpu(1)', 'cpu(2)', 'cpu(3)'],
['13:46:17', 0.8, 0.7, 1.1],
['13:46:24', 0.6, 0.5, 0.9],
['13:46:31', 0.6, 0.5, 0.8],
['13:46:38', 0.6, 0.5, 0.9],
['13:46:52', 0.5, 0.5, 0.8],
['13:46:59', 0.6, 0.5, 0.9],
['13:47:06', 0.6, 0.6, 0.9],
['13:47:13', 0.6, 0.5, 0.9]
スクリプト:
$ cat tst.awk
BEGIN {
OFS = ", "
}
!seenTimes[$1]++ {
times[++numTimes] = $1
}
!seenCpus[$3]++ {
cpus[++numCpus] = $3
}
{
vals[$1,$3] = $2
}
END {
printf "[\047%s\047%s", "Time", OFS
for ( cpuNr=1; cpuNr<=numCpus; cpuNr++ ) {
cpu = cpus[cpuNr]
printf "\047%s\047%s", cpu, (cpuNr<numCpus ? OFS : "]")
}
for ( timeNr=1; timeNr<=numTimes; timeNr++ ) {
time = times[timeNr]
printf ",%s[\047%s\047%s", ORS, time, OFS
for ( cpuNr=1; cpuNr<=numCpus; cpuNr++ ) {
cpu = cpus[cpuNr]
val = vals[time,cpu]
printf "%s%s", val, (cpuNr<numCpus ? OFS : "]")
}
}
print ""
}
メモ:スクリプト形式よりも簡単なスクリプトをリクエストしてください
答え1
$ cat tst.awk
BEGIN {
OFS = ", "
}
!seen[$1]++ {
times[++numTimes] = $1
}
!seen[$3]++ {
cpus[++numCpus] = $3
}
{
vals[$1,$3] = $2
}
END {
printf "[\047%s\047%s", "Time", OFS
for ( cpuNr=1; cpuNr<=numCpus; cpuNr++ ) {
cpu = cpus[cpuNr]
printf "\047%s\047%s", cpu, (cpuNr<numCpus ? OFS : "]")
}
for ( timeNr=1; timeNr<=numTimes; timeNr++ ) {
time = times[timeNr]
printf ",%s[\047%s\047%s", ORS, time, OFS
for ( cpuNr=1; cpuNr<=numCpus; cpuNr++ ) {
cpu = cpus[cpuNr]
val = ( (time,cpu) in vals ? vals[time,cpu] : prev_vals[cpu] )
printf "%s%s", val, (cpuNr<numCpus ? OFS : "]")
prev_vals[cpu] = val
}
}
print ""
}
$ awk -f tst.awk file
['Time', 'cpu(1)', 'cpu(2)', 'cpu(3)'],
['13:46:17', 0.8, 0.7, 1.1],
['13:46:24', 0.6, 0.5, 0.9],
['13:46:31', 0.6, 0.5, 0.8],
['13:46:38', 0.6, 0.5, 0.9],
['13:46:52', 0.5, 0.5, 0.8],
['13:46:59', 0.6, 0.5, 0.9],
['13:47:06', 0.6, 0.6, 0.9],
['13:47:13', 0.6, 0.5, 0.9]
スクリプト全体を1行にするには、各改行文字を;
。