Here’s a quick Bash script to prefix lines read from standard input by a time stamp.
#!/usr/bin/env bash while read line do echo $(date "$@") "$line" done
This script takes the same arguments as the traditional Unix date program. With no arguments, it prints some default-formatted date, but if you feel like it, you can pass it the same arguments as date. For example:
.../geolocalisation> cat /dev/ttyACM0 | ts.sh +"%H:%I:%M:%S.%N" | grep GPRMC | tee $(date | tr \ - | tr : - ).dat 15:03:42:57.355437828 $GPGGA,204226.000,4821.6737,N,06844.4790,W,1,08,1.2,-0.9,M,,,,00$GPRMC,204247.000,A,4821.6740,N,06844.4799,W,0.00,181.2,060215,,*2B 15:03:42:57.629415235 $GPRMC,204248.000,A,4821.6740,N,06844.4799,W,0.00,181.2,060215,,*24 15:03:42:57.881812555 $GPRMC,204249.000,A,4821.6740,N,06844.4800,W,0.00,181.2,060215,,*2A 15:03:42:58.150091204 $GPRMC,204250.000,A,4821.6740,N,06844.4800,W,0.00,181.2,060215,,*22 15:03:42:58.420285771 $GPRMC,204251.000,A,4821.6740,N,06844.4800,W,0.00,181.2,060215,,*23 15:03:42:58.682492676 $GPRMC,204252.000,A,4821.6740,N,06844.4800,W,0.00,181.2,060215,,*20
So Let’s examine the script more closely. The $@ passed as argument to date is the series of all arguments passed to the script. Why is it enclosed in quotes? Because it preserves the structures of the arguments passed to the script for date. In the specific case of date it might not be necessary because of the nature of its arguments, but should you replace date with some other command, it may cause problems to remove the quotes. The $line is also enclosed in quotes because otherwise the script misbehaves: it would, for example, try to expand *.