Add timestamp to cat output from shell script

回眸只為那壹抹淺笑 提交于 2019-12-05 19:59:36
PleaseStand

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 &
Kevin Beck

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

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 '...' &
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!