5

I have a small script that cats the output from the ttyUSB to a file I would like to prepend a timestamp to each line. From the command line this does everything I want:

$ cat /dev/ttyUSB0 /home/pi/daily_logs/ttyUSSB0 | awk '{ print strftime("%Y-%m-%d %H:%M:%S"), $0; }

My issue is that when I add it to a script everything works but the awk timestamp isn't added. My script line looks like this:

cat < /dev/ttyUSB0 > /home/pi/daily_logs/ttyUSB0 | awk '{ print strftime("%Y-%m-%d %H:%M:%S"), $0; } &

Any help getting this going would be appreciated.

Argus
  • 53
  • 1
  • 4

3 Answers3

8

You need to redirect awk's output to the file, not cat's. The way you have it, awk gets nothing. Actually, you may not need cat at all:

awk '{ print strftime("%Y-%m-%d %H:%M:%S"), $0; }' /dev/ttyUSB0 > /home/pi/daily_logs/ttyUSB0 &
Community
  • 1
  • 1
PleaseStand
  • 31,641
  • 6
  • 68
  • 95
  • Happy to push you over ;-) Good luck to all. – shellter Jan 04 '13 at 17:52
  • Awesome thank you, I have a lot more reading to do. Follow up question. Now that I have this running without cat, how to I stop it if I want to roll the file over at end of the day? Currently I am looking for the cat pids st – Argus Jan 07 '13 at 16:58
1

You need to put the > /home/pi/daily_logs/ttyUSB0 after the pipe. Like so:

cat < /dev/ttyUSB0 | awk '{ print strftime("%Y-%m-%d %H:%M:%S"), $0; }' > /home/pi/daily_logs/ttyUSB0

kkonrad
  • 1,262
  • 13
  • 32
Kevin Beck
  • 2,394
  • 2
  • 15
  • 27
0

Useless use of cat is impeding the solution. To minimize the changes needed, you can also do:

< /dev/ttyUSB0 > /home/pi/daily_logs/ttyUSB0 awk '...' &
William Pursell
  • 204,365
  • 48
  • 270
  • 300