awk 将特定行转置为列并对它们进行分组

awk 将特定行转置为列并对它们进行分组

我在标准输出中有以下数据:

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 行,只需将每个换行符替换为;.

相关内容