Command-Line

如何使用grep從目前行中減去前一行中的數字?

  • March 29, 2020

我使用 grep 來獲取mysqladminas的輸出

sudo mysqladmin ext -i10 | grep 'buffer_pool_pages_flushed'

並且輸出是連續的(每 10 秒)為

| Innodb_buffer_pool_pages_flushed                             | 265708726                                        |
| Innodb_buffer_pool_pages_flushed                             | 265735665                                        |
| Innodb_buffer_pool_pages_flushed                             | 265751712                                        |
| Innodb_buffer_pool_pages_flushed                             | 265754576                                        |
| Innodb_buffer_pool_pages_flushed                             | 265774380                                        |

如何調整grep命令以輸出第二列中連續數字之間的差異,例如

26939 (265735665-265708726)
16047 (265751712-265735665)
2864 (265754576-265751712)
19804 (265774380-265754576)

附加:

| awk '{if(NR>1){print $4-last,"("$4"-"last")"} last=$4}'

輸出:

26939 (265735665-265708726)
16047 (265751712-265735665)
2864 (265754576-265751712)
19804 (265774380-265754576)
awk '/buffer_pool_pages_flushed/{curr=$(NF-1); print curr-prev; prev=curr}'

引用自:https://unix.stackexchange.com/questions/576614