Search code examples
bashtimestampappendpuid

add timestamp to top command batch output


I need to add timestamp to the beginning of each line printed in the following output.

# top -b |grep -w 'db2sysc\|java'

 4756 db2inst1  20   0 2525m 875m 830m S    0 11.0   1:33.57 db2sysc
 4951 root      20   0 1416m 689m 7276 S    0  8.7   1:27.86 java
 4756 db2inst1  20   0 2525m 875m 830m S    1 11.0   1:33.61 db2sysc
 4951 root      20   0 1416m 689m 7276 S    0  8.7   1:27.87 java

would like it like this

1366138603 4756 db2inst1  20   0 2525m 875m 830m S    0 11.0   1:33.57 db2sysc
1366138603 4951 root      20   0 1416m 689m 7276 S    0  8.7   1:27.86 java
1366138692 4756 db2inst1  20   0 2525m 875m 830m S    1 11.0   1:33.61 db2sysc
1366138692 4951 root      20   0 1416m 689m 7276 S    0  8.7   1:27.87 java

Thank you for any help.


Solution

  • I'd suggest using awk instead of grep:

    top -b | awk '/db2sysc|java/ {print systime(), $0}'