我有这个文件:
Month,CPU,RAM
July 2018,19%,46%
August 2018,20%,45%
September 2018,20%,41%
October 2018,21%,39%
November 2018,21%,39%
December 2018,21%,41%
January 2019,25%,46%
February 2019,27%,50%
我需要计算第二列中的值之间的差,但要两两:
例如:
July -> August
CPU : +1% ( because 20-19)
August -> September
CPU : +0% ( because 20-20)
September -> October
CPU : +1% ( because 21-20)
我会在7月和8月进行尝试:
cat myfile.txt | egrep "July|August" | awk -F',' '{diff-=$2} END {print diff}'
但是结果是:
39
问题是我必须指定7月和8月,这效率不高,因为在其他月份也必须这样做。
有没有一种方法可以计算这些值之间的差异?我只需要知道值之间的差异(值2-值1,值3-值2-值2-值4-值3等等...),不一定要具有与示例中相同的表示。 / p>
请,你能告诉我吗?
谢谢!
答案 0 :(得分:3)
$ awk -F'[ ,%]' '
NR>3{print ""}
NR>2{printf "%s -> %s\nCPU : %+d%% (because %d-%d)\n",month,$1,$3-usage,$3,usage}
NR>1{month=$1;usage=$3}
' file
July -> August
CPU : +1% (because 20-19)
August -> September
CPU : +0% (because 20-20)
September -> October
CPU : +1% (because 21-20)
October -> November
CPU : +0% (because 21-21)
November -> December
CPU : +0% (because 21-21)
December -> January
CPU : +4% (because 25-21)
January -> February
CPU : +2% (because 27-25)
即使对于初学者来说,调整输出格式也非常容易。例如,如果您想要更紧凑的输出:
$ awk -F'[ ,%]' 'NR>2{printf "%-9s -> %-9s : %+3d%%\n",month,$1,$3-usage} NR>1{month=$1;usage=$3}' file
July -> August : +1%
August -> September : +0%
September -> October : +1%
October -> November : +0%
November -> December : +0%
December -> January : +4%
January -> February : +2%
答案 1 :(得分:1)
$ awk -F'[ ,]' '
NR>2{ printf "%s -> %s\nCPU : %+d%% ( because %d-%d)\n\n", p[1], $1, $3-p[3], $3, p[3] }
{ split($0,p) }
' file
July -> August
CPU : +1% ( because 20-19)
August -> September
CPU : +0% ( because 20-20)
September -> October
CPU : +1% ( because 21-20)
October -> November
CPU : +0% ( because 21-21)
November -> December
CPU : +0% ( because 21-21)
December -> January
CPU : +4% ( because 25-21)
January -> February
CPU : +2% ( because 27-25)
答案 2 :(得分:0)
另一个awk
:
awk -F'[ ,%]' '
FNR>2{print m " -> " $1;printf "CPU : %+d%%%s",$3-u,ORS}
{m=$1;u=$3}
' file
输出
July -> August
CPU : +1%
August -> September
CPU : +0%
September -> October
CPU : +1%
October -> November
CPU : +0%
November -> December
CPU : +0%
December -> January
CPU : +4%
January -> February
CPU : +2%