appending timestamp to tail -f results
--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Music Box Puzzles
--
Chapters
00:00 Appending Timestamp To Tail -F Results
00:22 Answer 1 Score 6
00:45 Answer 2 Score 0
00:55 Accepted Answer Score 25
01:14 Answer 4 Score 1
01:32 Thank you
--
Full question
https://superuser.com/questions/298347/a...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#tail #bashscripting
#avk47
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Music Box Puzzles
--
Chapters
00:00 Appending Timestamp To Tail -F Results
00:22 Answer 1 Score 6
00:45 Answer 2 Score 0
00:55 Accepted Answer Score 25
01:14 Answer 4 Score 1
01:32 Thank you
--
Full question
https://superuser.com/questions/298347/a...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#tail #bashscripting
#avk47
ACCEPTED ANSWER
Score 25
It probably doesn't get any shorter than this:
tail -f outputfile | xargs -IL date +"%Y%m%d_%H%M%S:L"
Note that I used the timestamp format suggested by your question, but you're free to use anything supported by the date
command (the format syntax is supported by standard BSD and GNU date).
ANSWER 2
Score 6
Write a simple script to do that. Here's an example in perl:
#!/usr/bin/perl
while(<>) {
print localtime . ": $_";
}
To use the script, simply pipe your tail output through it:
tail -f outputfile | ./prepend_timestamp.pl
You could also do it inline:
tail -f outputfile | perl -pe '$_ = localtime.": $_"'
ANSWER 3
Score 1
I would normally do this with perl as answered by others, but it was unavailable at the rather trimmed down Linux machine where I needed it, but the busybox awk applet was!
tail -f some_file | awk -e '{ print strftime("%Y%m%d_%H%M%S",systime()) "\t" $0}'
As always the timestamp is for when the line was printed, not when it was written originally.
ANSWER 4
Score 0
With awk:
tail -f infile | awk '{"date \"+%Y%m%d_%H%M%S\"" | getline now} {close("date")} {print now ": " $0}'