Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append a timestamp to each line as it comes out of grep?

I have an infinite stream of data coming out of a logger, which I am piping to grep. I would like to save the output of the grep to a file, but also include a timestamp at the beginning of each line (the time at which the line appeared). Is there an easy way to accomplish this? Assume I cannot change the output of the logger process.

like image 871
Tim Avatar asked Jul 30 '12 23:07

Tim


People also ask

How do you grep multiple lines from output?

The basic grep syntax when searching multiple patterns in a file includes using the grep command followed by strings and the name of the file or its path. The patterns need to be enclosed using single quotes and separated by the pipe symbol. Use the backslash before pipe | for regular expressions.

How do you grep and append?

If you wish to append the output at the end of the file, use >> rather than > as the redirection operator. What this actually does is to start cat and grep concurrently. cat will read from q1. txt and try to write it to its standard output, which is connected to the standard input of grep .

How do I add a timestamp in Linux?

Script to append date stamp to file:#!/bin/sh file_name=test_files. txt current_time=$(date "+%Y. %m. %d-%H.

What does '- Q do in grep?

grep defines success as matching 1 or more lines. Failure includes matching zero lines, or some other error that prevented matching from taking place in the first place. -q is used when you don't care about which lines matched, only that some lines matched.


1 Answers

You can append a static timestamp using sed and date:

... | sed "s/^/$(date) /" >> output.txt

Alternatively, if you require a realtime timestamp, use gawk's strftime function:

... | gawk '{ print strftime(), $0 }'

You can define your favourite formatting:

... | gawk '{ print strftime("%Y-%m-%d %H:%M:%S"), $0 }'

And if buffering is a problem, don't forget to flush each line:

... | awk '{ print strftime("%Y-%m-%d %H:%M:%S"), $0; fflush() }'

Alternatively, use unbuffer:

unbuffer ... | awk '{ print strftime("%Y-%m-%d %H:%M:%S"), $0 }'

If you don't have gawk, you have a couple of other options:

(a) Install ts (from moreutils):

... | ts '%F %T'

(b) Use perl:

... | perl -pe 's/^/localtime . " "/e'

or with formatting:

... | perl -MPOSIX -pe 's/^/strftime("%Y-%m-%d %H:%M:%S", localtime) . " "/e'

Don't forget that you can use gmtime instead of localtime if you need GMT formatted to your locale.

(c) Ask a question.

like image 67
Steve Avatar answered Oct 15 '22 14:10

Steve