awk脚本,统计+value和-value的个数

Sae*_*awi 1 linux awk

在下面的列表中是每天的价格Benzin,我希望每一天与前一天的差异(第一天没有出现在输出中)

  • 在声明的结尾:价格上涨、保持不变或不可用的天数。
list of benzin prise
Run Code Online (Sandbox Code Playgroud)
01.05.2021 156
02.05.2021 154
03.05.2021 151
04.05.2021 152
05.05.2021 148
06.05.2021 149
07.05.2021 147
08.05.2021 150
09.05.2021 150
10.05.2021 152
11.05.2021 157
12.05.2021 151
13.05.2021 147
14.05.2021 144
15.05.2021 150
Run Code Online (Sandbox Code Playgroud)

输出应如下所示:

•   02.05.2021: -2
•   03.05.2021: -3
•   04.05.2021: 1
•   05.05.2021: -4
•   06.05.2021: 1
•   07.05.2021: -2
•   08.05.2021: 3
•   09.05.2021: 0
•   10.05.2021: 2
•   11.05.2021: 5
•   12.05.2021: -6
•   13.05.2021: -4
•   14.05.2021: -3
•   15.05.2021: 6
•   
•   Number increased: 6
•   Number missing or equal: 8
Run Code Online (Sandbox Code Playgroud)

我试过这段代码,但仍然有些不完全,我正在研究它:

awk 'NR>2 {print $1 " " $2-p} {p=$2}' <benzinprise

我仍然需要计算增加和不增加的数字。

Rav*_*h13 5

仅使用您显示的示例,请尝试以下操作。

awk '
FNR>1{
  diff=($2-prevVal)
  if(diff>0){ increase++ }
  else      { missing++  }
  print $1": "diff
}
{
  prevVal=$2
}
END{
  print "Number increased: "increase+0 ORS "Number missing or equal: " missing+0
}
'  Input_file
Run Code Online (Sandbox Code Playgroud)

说明:为以上添加详细说明。

awk '                         ##Starting awk program from here.
FNR>1{                        ##If line is not first line then do following.
  diff=($2-prevVal)           ##Taking difference of 2nd column and prevVal here.
  if(diff>0){ increase++ }    ##If diff is greater than 0 then increase variable with 1
  else      { missing++  }    ##Else increase missing with 1 here.
  print $1": "diff            ##Printing 1st column with diff here.
}
{
  prevVal=$2                  ##Setting prevVal to 2nd column value here.
}
END{                          ##Starting END block of this program from here.
                              ##Printing number of increased or missing/decreased values.
  print "Number increased: "increase+0 ORS "Number missing or equal: " missing+0
}
'  Input_file                 ##Mentioning Input_file name here. 
Run Code Online (Sandbox Code Playgroud)