计算与平面文件的差异

发布于 2024-08-19 18:01:09 字数 610 浏览 3 评论 0原文

我有一个文本文件,最后两行看起来像这样...

Uptime: 822832  Threads: 32  Questions: 13591705  Slow queries: 722  Opens: 81551  Flush tables: 59  Open tables: 64  Queries per second avg: 16.518
Uptime: 822893  Threads: 31  Questions: 13592768  Slow queries: 732  Opens: 81551  Flush tables: 59  Open tables: 64  Queries per second avg: 16.618

如何找到每个参数的两个值之间的差异? 预期输出是:

61 -1 1063 10 0 0 0 0.1

换句话说,我想从早期的正常运行时间中扣除当前的正常运行时间值。 找出线索和问题之间的区别等等。

此练习的目的是观察此文件并在差异太大时提醒用户。例如,如果慢查询超过 500 或“Questions”参数太低(<100)

(这是 MySQL 状态,但与之无关,因此 mysql 标签不适用)

I have a text file and the last 2 lines look like this...

Uptime: 822832  Threads: 32  Questions: 13591705  Slow queries: 722  Opens: 81551  Flush tables: 59  Open tables: 64  Queries per second avg: 16.518
Uptime: 822893  Threads: 31  Questions: 13592768  Slow queries: 732  Opens: 81551  Flush tables: 59  Open tables: 64  Queries per second avg: 16.618

How do I find the difference between the two values of each parameter?
The expected output is:

61 -1 1063 10 0 0 0 0.1

In other words I will like to deduct the current uptime value from the earlier uptime.
Find the difference between the threads and Questions and so on.

The purpose of this exercise is to watch this file and alert the user when the difference is too high. For e.g. if the slow queries are more than 500 or the "Questions" parameter is too low (<100)

(It is the MySQL status but has nothing to do with it, so mysql tag does not apply)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

世态炎凉 2024-08-26 18:01:09

与 Ghostdog74 的(原始)答案略有不同:

tail -2 file | awk ' {
  gsub(/[a-zA-Z: ]+/," ")
  m=split($0,a," ");
  for (i=1;i<=m;i++)
    if (NR==1) b[i]=a[i]; else print a[i] - b[i]
} '

Just a slight variation on ghostdog74's (original) answer:

tail -2 file | awk ' {
  gsub(/[a-zA-Z: ]+/," ")
  m=split($0,a," ");
  for (i=1;i<=m;i++)
    if (NR==1) b[i]=a[i]; else print a[i] - b[i]
} '
信愁 2024-08-26 18:01:09

这是一种方法。 tail 用于获取最后两行,如果您有一个大文件,则在效率方面特别有用。

tail -2 file | awk '
{
 gsub(/[a-zA-Z: ]+/," ")
 m=split($0,a," ")
 if (f) {
   for (i=1;i<=m;i++){
        print -(b[i]-a[i])
    }
   # to check for Questions, slow queries etc
   if (  -(b[3]-a[3])  < 100 ){
      print "Questions parameter too low"
   }else if ( -(b[4]-a[4]) > 500 ){
      print "Slow queries more than 500"
   }else if ( a[1] - b[1] < 0 ){
      print "mysql ...... "
   }
    exit
 }
 for(i=1;i<=m;i++ ){ b[i]=a[i] ;f=1 }
} '

输出

$ ./shell.sh
61
-1
1063
10
0
0
0
0.1

here's one way. tail is used to get the last 2 lines, especially useful in terms of efficiency if you have a big file.

tail -2 file | awk '
{
 gsub(/[a-zA-Z: ]+/," ")
 m=split($0,a," ")
 if (f) {
   for (i=1;i<=m;i++){
        print -(b[i]-a[i])
    }
   # to check for Questions, slow queries etc
   if (  -(b[3]-a[3])  < 100 ){
      print "Questions parameter too low"
   }else if ( -(b[4]-a[4]) > 500 ){
      print "Slow queries more than 500"
   }else if ( a[1] - b[1] < 0 ){
      print "mysql ...... "
   }
    exit
 }
 for(i=1;i<=m;i++ ){ b[i]=a[i] ;f=1 }
} '

output

$ ./shell.sh
61
-1
1063
10
0
0
0
0.1
此刻的回忆 2024-08-26 18:01:09

呆呆地:

BEGIN {
  arr[1] = "0"
}
length(arr) > 1 {
  print $2-arr[1], $4-arr[2], $6-arr[3], $9-arr[4], $11-arr[5], $14-arr[6], $17-arr[7], $22-arr[8]
}
{
  arr[1] = $2
  arr[2] = $4
  arr[3] = $6
  arr[4] = $9
  arr[5] = $11
  arr[6] = $14
  arr[7] = $17
  arr[8] = $22
}

gawk:

BEGIN {
  arr[1] = "0"
}
length(arr) > 1 {
  print $2-arr[1], $4-arr[2], $6-arr[3], $9-arr[4], $11-arr[5], $14-arr[6], $17-arr[7], $22-arr[8]
}
{
  arr[1] = $2
  arr[2] = $4
  arr[3] = $6
  arr[4] = $9
  arr[5] = $11
  arr[6] = $14
  arr[7] = $17
  arr[8] = $22
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文