I recently needed exactly that: receive log messages in a serial console (picocom), print them to a terminal and to a file AND prepend the date.
What I now use looks s.th. like this:
picocom -b 115200 /dev/tty.usbserial-1a122C | awk '{ print strftime("%s: "), $0; fflush(); }' | tee serial.txt
- the output of
picocom
is piped toawk
awk
prepends the date (the%s
option converts the time to the Number of seconds since 1970-01-01 00:00:00 UTC – or use%c
for a human-readable format)fflush()
flushes any buffered output inawk
- that is piped to
tee
which diverts it to a file. (you can find some stuff abouttee
here)