开发者

sed, environment variable and date problem

开发者 https://www.devze.com 2023-01-10 03:53 出处:网络
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&

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.


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?


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

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

精彩评论

暂无评论...
验证码 换一张
取 消