I'm trying to prepend the current time to a few columns pulled from each line of a growing text file, and print to the screen.
tail -f dumpfile.txt|awk '{ "date +%T"|getline curtime;print curtime, $2, $7 }'
A sample input line from the file would be:
N 7056 65433 flags 0dbc a000 OK 0
Above command prints each line from tail, however it executes date
just once, time that the command line is launched:
Desired Actual
13:10:05 7050 OK 13:10:04 7050 OK
13:10:05 7051 OK 13:10:04 7051 OK
13:10:06 7052 OK 13:10:04 7052 OK
13:10:06 7053 OK 13:10:04 7053 OK
13:10:07 7054 OK 13:10:04 7054 OK
13:10:07 7055 OK 13:10:04 7055 OK
13:10:08 7056 OK 13:10:04 7056 OK
How can I rewrite this so that the date command is executed each time a new line comes in?
Thanks.
The first getline
exhausts date
's output (as it's just a single line), subsequent calls to getline
fail and leave curtime
unchanged. You need to close the file descriptor associated with date +%T
every time you read from it, e.g:
tail -f dumpfile.txt | awk 'BEGIN{cmd="date +%T"} {cmd|getline curtime; print curtime,$2,$7; close(cmd)}'