linux - 将时间戳添加到 shell 脚本的 cat 输出

标签 linux shell awk cat

我有一个小脚本,可以将 ttyUSB 的输出转换为一个文件,我想在每一行前加上一个时间戳。从命令行这做我想做的一切:

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

我的问题是,当我将它添加到脚本时,一切正常,但未添加 awk 时间戳。我的脚本行如下所示:

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

如能提供帮助,我们将不胜感激。

最佳答案

您需要将 awk 的输出重定向到文件,而不是 cat 的。按照你的方式,awk 什么也得不到。实际上,you may not need cat at all :

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

关于linux - 将时间戳添加到 shell 脚本的 cat 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14161552/

相关文章:

c - 无操作系统执行

linux - 微型计算机 : AT+CMGR=n not showing the sms content

linux - inotify - 最好的方法是什么?

shell - 如何返回sqlite3 shell中的提示?

c++ - Execvp 仅正确运行第一个命令

css - ttf2eot - Mac OS X 上的输入路径是什么?

linux - 使用分隔符拆分字符串

linux - 在 Linux 中的同一文件中执行某些操作后需要添加换行符

linux - Bash 脚本终止于 grep 语句

json - 在bash中用awk/sed解析json得到键值对