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.
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.
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 .
Script to append date stamp to file:#!/bin/sh file_name=test_files. txt current_time=$(date "+%Y. %m. %d-%H.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With