sed,环境变量和日期问题

发布于 2024-09-13 04:49:14 字数 448 浏览 1 评论 0原文

我想向服务器事件添加时间戳并将结果存储在日志中。

我的第一个想法是:

( ./runServer.sh ) | sed "s/.*/`date +%s` & /" | xargs -0  >Server.log 2>&1  &

但 sed 似乎永远不会重新评估日期,因此所有事件都有相同的时间戳。

现在我正在尝试使用环境变量来解决这个问题,但我找不到合适的方法来做到这一点。

我下面有明显错误的一行:

( ./runServer.sh ) | xargs -0 'export mydate=`date +%s` ; sed "s/.*/$mydate & /"' >Server.log 2>&1  &

有任何提示吗?谢谢。

I want to add a timestamp to server events and store the result in a log.

My first idea was :

( ./runServer.sh ) | sed "s/.*/`date +%s` & /" | xargs -0  >Server.log 2>&1  &

But it seems sed never reevaluates the date, so all events get the same timestamp.

Now I'm trying to get around that using environment variable but I can't find a proper way to do it.

I have this obviously wrong line below :

( ./runServer.sh ) | xargs -0 'export mydate=`date +%s` ; sed "s/.*/$mydate & /"' >Server.log 2>&1  &

Any hints? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

木格 2024-09-20 04:49:14

试试这个:

<command> | awk '{ print strftime("%Y-%m-%d %H:%M:%S"), $0; }'

酱汁: 有吗Unix 实用程序将时间戳添加到文本行?

Try this:

<command> | awk '{ print strftime("%Y-%m-%d %H:%M:%S"), $0; }'

Sauce: Is there a Unix utility to prepend timestamps to lines of text?

雨的味道风的声音 2024-09-20 04:49:14

逐步执行,并使用 $() 而不是反引号。试试这个,没测试过。

timestamp=$(date +%s)
./runServer.sh | sed "s/.*/$timestamp & /" | ....... 

do it step by step, and use $() instead of backticks. Try this, not tested.

timestamp=$(date +%s)
./runServer.sh | sed "s/.*/$timestamp & /" | ....... 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文