Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

need to insert datetime in every row of vmstat output

Tags:

bash

ksh

perl

I need to insert datetime in every vmstat line that has value.

I can create a function like this:

function insert_datetime {
  while read line
  do
    printf "$line"
    date '+ %m-%d-%Y %H:%M:%S'
  done
}

then call vmstat as below:

'vmstat 3 5 | insert_datetime'

but this line puts date time to every line, including dashes (--) and any rows that has text. How can I exclude rows that has dahses and text?

kthr    memory              page              faults              cpu 04-23-2013 10:19:49
----- ----------- ------------------------ ------------ ----------------------- 04-23-2013 10:19:49
r  b   avm   fre  re  pi  po  fr   sr  cy  in   sy  cs us sy id wa    pc    ec 04-23-2013 10:19:49
0  0 45688088 4094129   0   0   0   0    0   0  45 12172 2840  1  1 99  0  0.35   2.2 04-23-2013 10:19:49
2  0 45694135 4088082   0   0   0   0    0   0 451 56350 21818  3  1 97  0  0.73   4.5 04-23-2013 10:19:52
1  0 45694137 4088061   0   0   0   0    0   0 303 24568 951  3  1 96  0  0.82   5.1 04-23-2013 10:19:55
1  0 45694138 4087739   0   0   0   0    0   0 445 9170 1504  2  0 98  0  0.64   4.0 04-23-2013 10:19:58
4  0 45703145 4078732   0   0   0   0    0   0 335 47175 1306  4  1 95  0  1.01   6.3 04-23-2013 10:20:01

I needed to look like this:

kthr    memory              page              faults              cpu
----- ----------- ------------------------ ------------ ----------------------- 
r  b   avm   fre  re  pi  po  fr   sr  cy  in   sy  cs us sy id wa    pc    ec 
0  0 45688088 4094129   0   0   0   0    0   0  45 12172 2840  1  1 99  0  0.35   2.2 04-23-2013 10:19:49
2  0 45694135 4088082   0   0   0   0    0   0 451 56350 21818  3  1 97  0  0.73   4.5 04-23-2013 10:19:52
1  0 45694137 4088061   0   0   0   0    0   0 303 24568 951  3  1 96  0  0.82   5.1 04-23-2013 10:19:55
1  0 45694138 4087739   0   0   0   0    0   0 445 9170 1504  2  0 98  0  0.64   4.0 04-23-2013 10:19:58
4  0 45703145 4078732   0   0   0   0    0   0 335 47175 1306  4  1 95  0  1.01   6.3 04-23-2013 10:20:01
like image 618
user1471980 Avatar asked Dec 04 '25 00:12

user1471980


2 Answers

Why not just use vmstat -t? It seems to be exactly what you are looking for. Here is some sample output

[root@web5 vmstat]# vmstat -t 1
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------ ---timestamp---
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0  15704 193236 189628 595868    9    3    25    16   15   20 11  1 88  1  0    2013-05-22 13:32:36 JST
 0  0  15704 193212 189628 595868    0    0     0     0   22   20  0  0 100  0  0   2013-05-22 13:32:37 JST
 0  0  15704 193212 189628 595868    0    0     0     0   19   12  0  0 100  0  0   2013-05-22 13:32:38 JST
 0  0  15704 193212 189628 595868    0    0     0     0   10   11  0  0 100  0  0   2013-05-22 13:32:39 JST
 0  0  15704 193212 189628 595868    0    0     0    96   34   25  0  1 99  0  0    2013-05-22 13:32:40 JST
 0  0  15704 193212 189628 595868    0    0     0     0   10    9  0  0 100  0  0   2013-05-22 13:32:41 JST
 0  0  15704 193212 189628 595868    0    0     0     0   14   23  0  0 100  0  0   2013-05-22 13:32:42 JST

executed on CentOS6.3 with procps 3.2.8

[root@web5 uptime]# vmstat -V
procps version 3.2.8
like image 116
paullb Avatar answered Dec 06 '25 15:12

paullb


Use awk:

vmstat 3 5 | awk '/^ *[0-9]/{$0=$0 " " strftime("%m-%d-%Y %T")};1'
like image 34
Ansgar Wiechers Avatar answered Dec 06 '25 15:12

Ansgar Wiechers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!