在 bash 中将 stat/number 与另一个 stat/number 相除
Make a division of a stat/number from another stat/number in bash
我有一个简单的问题,但我无法使用 bash,我正在使用命令行来获取我的 Pi-Hole 对 Unbound(递归 DNS ) 并且我想将缓存的查询显示为总查询的百分比,这里是获取总查询的行:
sudo unbound-control stats_noreset | awk -F '=' '/total.num.queries/ {print $NF}'
例如,这给了我 1500
我想用这条线除以这个数字:
sudo unbound-control stats_noreset | awk -F '=' '/total.num.cachehits/ {print $NF}'
例如 500
我想将其显示为:
500 33.3%
只有一行代码,没有变量。
非常感谢!我试了好几天。
编辑:根据要求,sudo unbound-control stats_noreset 的完整输出是:
sudo unbound-control stats_noreset
thread0.num.queries=1294
thread0.num.queries_ip_ratelimited=0
thread0.num.cachehits=327
thread0.num.cachemiss=967
thread0.num.prefetch=134
thread0.num.zero_ttl=0
thread0.num.recursivereplies=967
thread0.requestlist.avg=0.334242
thread0.requestlist.max=9
thread0.requestlist.overwritten=0
thread0.requestlist.exceeded=0
thread0.requestlist.current.all=0
thread0.requestlist.current.user=0
thread0.recursion.time.avg=0.080698
thread0.recursion.time.median=0.0325689
thread0.tcpusage=0
thread1.num.queries=1309
thread1.num.queries_ip_ratelimited=0
thread1.num.cachehits=342
thread1.num.cachemiss=967
thread1.num.prefetch=132
thread1.num.zero_ttl=0
thread1.num.recursivereplies=967
thread1.requestlist.avg=0.374886
thread1.requestlist.max=9
thread1.requestlist.overwritten=0
thread1.requestlist.exceeded=0
thread1.requestlist.current.all=0
thread1.requestlist.current.user=0
thread1.recursion.time.avg=0.075309
thread1.recursion.time.median=0.0322503
thread1.tcpusage=0
thread2.num.queries=1338
thread2.num.queries_ip_ratelimited=0
thread2.num.cachehits=336
thread2.num.cachemiss=1002
thread2.num.prefetch=156
thread2.num.zero_ttl=0
thread2.num.recursivereplies=1002
thread2.requestlist.avg=0.360104
thread2.requestlist.max=9
thread2.requestlist.overwritten=0
thread2.requestlist.exceeded=0
thread2.requestlist.current.all=0
thread2.requestlist.current.user=0
thread2.recursion.time.avg=0.073632
thread2.recursion.time.median=0.031425
thread2.tcpusage=0
thread3.num.queries=1258
thread3.num.queries_ip_ratelimited=0
thread3.num.cachehits=339
thread3.num.cachemiss=919
thread3.num.prefetch=127
thread3.num.zero_ttl=0
thread3.num.recursivereplies=919
thread3.requestlist.avg=0.315488
thread3.requestlist.max=9
thread3.requestlist.overwritten=0
thread3.requestlist.exceeded=0
thread3.requestlist.current.all=0
thread3.requestlist.current.user=0
thread3.recursion.time.avg=0.073834
thread3.recursion.time.median=0.0308651
thread3.tcpusage=0
total.num.queries=5199
total.num.queries_ip_ratelimited=0
total.num.cachehits=1344
total.num.cachemiss=3855
total.num.prefetch=549
total.num.zero_ttl=0
total.num.recursivereplies=3855
total.requestlist.avg=0.34673
total.requestlist.max=9
total.requestlist.overwritten=0
total.requestlist.exceeded=0
total.requestlist.current.all=0
total.requestlist.current.user=0
total.recursion.time.avg=0.075873
total.recursion.time.median=0.0317773
total.tcpusage=0
time.now=1613041718.040611
time.up=14305.501526
time.elapsed=14305.501526
thread0,1,etc..是核心,但我只对总数感兴趣。
我假设当你说没有变量时,你的意思是 在 shell 中没有变量。
考虑到这一点,您可以使用 awk 变量来存储中间结果:
sudo unbound-control stats_noreset | awk -F '=' ' == "total.num.queries" {queries=$NF} == "total.num.cachehits" {hits=$NF}END{print hits, hits/queries*100"%"}'
或更易读的多行格式:
sudo unbound-control stats_noreset |
awk -F '=' ' == "total.num.queries" { queries = $NF }
== "total.num.cachehits" { hits = $NF }
END { print hits, hits / queries * 100 "%" }'
输出为:
1344 25.8511%
如果输出只需要一个小数位,可以使用printf
,比如
END { printf "%d %.1f%%\n", hits, hits / queries * 100 }
我有一个简单的问题,但我无法使用 bash,我正在使用命令行来获取我的 Pi-Hole 对 Unbound(递归 DNS ) 并且我想将缓存的查询显示为总查询的百分比,这里是获取总查询的行:
sudo unbound-control stats_noreset | awk -F '=' '/total.num.queries/ {print $NF}'
例如,这给了我 1500
我想用这条线除以这个数字:
sudo unbound-control stats_noreset | awk -F '=' '/total.num.cachehits/ {print $NF}'
例如 500
我想将其显示为:
500 33.3%
只有一行代码,没有变量。
非常感谢!我试了好几天。
编辑:根据要求,sudo unbound-control stats_noreset 的完整输出是:
sudo unbound-control stats_noreset
thread0.num.queries=1294
thread0.num.queries_ip_ratelimited=0
thread0.num.cachehits=327
thread0.num.cachemiss=967
thread0.num.prefetch=134
thread0.num.zero_ttl=0
thread0.num.recursivereplies=967
thread0.requestlist.avg=0.334242
thread0.requestlist.max=9
thread0.requestlist.overwritten=0
thread0.requestlist.exceeded=0
thread0.requestlist.current.all=0
thread0.requestlist.current.user=0
thread0.recursion.time.avg=0.080698
thread0.recursion.time.median=0.0325689
thread0.tcpusage=0
thread1.num.queries=1309
thread1.num.queries_ip_ratelimited=0
thread1.num.cachehits=342
thread1.num.cachemiss=967
thread1.num.prefetch=132
thread1.num.zero_ttl=0
thread1.num.recursivereplies=967
thread1.requestlist.avg=0.374886
thread1.requestlist.max=9
thread1.requestlist.overwritten=0
thread1.requestlist.exceeded=0
thread1.requestlist.current.all=0
thread1.requestlist.current.user=0
thread1.recursion.time.avg=0.075309
thread1.recursion.time.median=0.0322503
thread1.tcpusage=0
thread2.num.queries=1338
thread2.num.queries_ip_ratelimited=0
thread2.num.cachehits=336
thread2.num.cachemiss=1002
thread2.num.prefetch=156
thread2.num.zero_ttl=0
thread2.num.recursivereplies=1002
thread2.requestlist.avg=0.360104
thread2.requestlist.max=9
thread2.requestlist.overwritten=0
thread2.requestlist.exceeded=0
thread2.requestlist.current.all=0
thread2.requestlist.current.user=0
thread2.recursion.time.avg=0.073632
thread2.recursion.time.median=0.031425
thread2.tcpusage=0
thread3.num.queries=1258
thread3.num.queries_ip_ratelimited=0
thread3.num.cachehits=339
thread3.num.cachemiss=919
thread3.num.prefetch=127
thread3.num.zero_ttl=0
thread3.num.recursivereplies=919
thread3.requestlist.avg=0.315488
thread3.requestlist.max=9
thread3.requestlist.overwritten=0
thread3.requestlist.exceeded=0
thread3.requestlist.current.all=0
thread3.requestlist.current.user=0
thread3.recursion.time.avg=0.073834
thread3.recursion.time.median=0.0308651
thread3.tcpusage=0
total.num.queries=5199
total.num.queries_ip_ratelimited=0
total.num.cachehits=1344
total.num.cachemiss=3855
total.num.prefetch=549
total.num.zero_ttl=0
total.num.recursivereplies=3855
total.requestlist.avg=0.34673
total.requestlist.max=9
total.requestlist.overwritten=0
total.requestlist.exceeded=0
total.requestlist.current.all=0
total.requestlist.current.user=0
total.recursion.time.avg=0.075873
total.recursion.time.median=0.0317773
total.tcpusage=0
time.now=1613041718.040611
time.up=14305.501526
time.elapsed=14305.501526
thread0,1,etc..是核心,但我只对总数感兴趣。
我假设当你说没有变量时,你的意思是 在 shell 中没有变量。
考虑到这一点,您可以使用 awk 变量来存储中间结果:
sudo unbound-control stats_noreset | awk -F '=' ' == "total.num.queries" {queries=$NF} == "total.num.cachehits" {hits=$NF}END{print hits, hits/queries*100"%"}'
或更易读的多行格式:
sudo unbound-control stats_noreset |
awk -F '=' ' == "total.num.queries" { queries = $NF }
== "total.num.cachehits" { hits = $NF }
END { print hits, hits / queries * 100 "%" }'
输出为:
1344 25.8511%
如果输出只需要一个小数位,可以使用printf
,比如
END { printf "%d %.1f%%\n", hits, hits / queries * 100 }