想要使用Gawk Bash添加游戏机输出的时刻毫秒

发布于 2025-02-10 00:25:39 字数 136 浏览 1 评论 0原文

我正在使用以下片段将时间戳添加到控制台输出中

command | gawk '{ print strftime("[%Y-%m-%d %H:%M:%S]"), $0 }'  

如何向其添加毫秒?

I am using following snippet to add timestamp to console output

command | gawk '{ print strftime("[%Y-%m-%d %H:%M:%S]"), $0 }'  

how to add milliseconds to it?

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

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

发布评论

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

评论(3

心房的律动 2025-02-17 00:25:39

为什么不一个简单的bash解决方案?您可以使用date命令读取每行,并在时间戳记中进行预示例,例如

command | while read -r line; do printf "%s %s\n" "$(date +'%F %T.%03N')" "$line"; done

使用/输出

示例

$ cat dat/captnjack.txt
This is a tale
Of Captain Jack Sparrow
A Pirate So Brave
On the Seven Seas.

timestamped output :(

$ cat dat/captnjack.txt | 
while read line; do printf "%s %s\n" "$(date +'%F %T.%03N')" "$line"; done
2022-06-24 00:45:28.030 This is a tale
2022-06-24 00:45:28.031 Of Captain Jack Sparrow
2022-06-24 00:45:28.033 A Pirate So Brave
2022-06-24 00:45:28.035 On the Seven Seas.

注:谢谢您@jhnc for GNU date %03N specifier)

带有bash您还可以使用 process替换来喂食时间戳循环,例如

while read -r line; do 
  printf "%s %s\n" "$(date +'%F %T.%03N')" "$line"
done < <(command)

Why not a simple bash solution? You can read each line and prepend a timestamp using the date command, e.g.

command | while read -r line; do printf "%s %s\n" "$(date +'%F %T.%03N')" "$line"; done

Example Use/Output

Sample lines:

$ cat dat/captnjack.txt
This is a tale
Of Captain Jack Sparrow
A Pirate So Brave
On the Seven Seas.

Timestamped output:

$ cat dat/captnjack.txt | 
while read line; do printf "%s %s\n" "$(date +'%F %T.%03N')" "$line"; done
2022-06-24 00:45:28.030 This is a tale
2022-06-24 00:45:28.031 Of Captain Jack Sparrow
2022-06-24 00:45:28.033 A Pirate So Brave
2022-06-24 00:45:28.035 On the Seven Seas.

(note: thank you @jhnc for the GNU date %03N specifier)

With bash you can also use process substitution to feed the timestamp loop, e.g.

while read -r line; do 
  printf "%s %s\n" "$(date +'%F %T.%03N')" "$line"
done < <(command)
無處可尋 2025-02-17 00:25:39

返回原始日期想法:

 echo -ne "[$( date '+%Y-%m-%d %H:%M:%S,%3N')] "; command

Coming back to the original date idea:

 echo -ne "[$( date '+%Y-%m-%d %H:%M:%S,%3N')] "; command
愛上了 2025-02-17 00:25:39

@RoHit:除非我缺少gawk手册中的东西,否则我看不到它,除非您要使用gawk Extension time。否则,这就是我为我的各种awk s获得microsecs的方式:

 {m,g}awk '{ OFMT="%.15f"

   print substr("",(__="gdate +\47%s.%N\47" \         # gnu-date
                    )|getline _,    close(__)) +_ }' 

   1656047978.467053890228271
  • 它只有53位的精度(或52,取决于您的身份询问隐含的位),所以这些额外的小数点仅仅是错觉

  • “ substr() “这里既不输出任何东西,也不影响返回的内容。它的唯一功能是充当各种临时分期

|

 ( I use substr() encapsulations as a generic way to 
   swap values between 2 variables w/o

     using a temp var, 
     an external function call, or 
     bit-wise XOR techniques (awk specs intentionally dont offer it)

   in a sense, it kinda gives a sorta similar,
   but certainly not identical,
   feel to more modern languages returning tuples)
  

也就是说,如果您不受仅限gawk的要求,我知道awk的1个变体提供了MilliSec Times而不会外部的时间:

 mawk2 '{ print substr("",srand(),srand()) srand() }'

 #srand1656049345.921576#

@Rohit : unless I'm missing something from the gawk manual, I don't see it, unless you want to use the gawk extension time. Otherwise, this is how I get microsecs for my various awks :

 {m,g}awk '{ OFMT="%.15f"

   print substr("",(__="gdate +\47%s.%N\47" \         # gnu-date
                    )|getline _,    close(__)) +_ }' 

   1656047978.467053890228271
  • It only has 53-bit of precision (or 52, depending on who you ask about the implied bit), so those extra decimal points are mere optical illusions

  • The "substr()" here neither outputs anything nor affect what's being returned. It's sole functionality is to act as a temporary staging area of sorts

|

 ( I use substr() encapsulations as a generic way to 
   swap values between 2 variables w/o

     using a temp var, 
     an external function call, or 
     bit-wise XOR techniques (awk specs intentionally dont offer it)

   in a sense, it kinda gives a sorta similar,
   but certainly not identical,
   feel to more modern languages returning tuples)
  

That said, if you aren't constrained by gawk-only requirement, i'm aware of 1 variant of awk that offers millisec times without going external :

 mawk2 '{ print substr("",srand(),srand()) srand() }'

 #srand1656049345.921576#
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文