打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
[20201121]显示时间戳高精度版本.txt

[20201121]显示时间戳高精度版本.txt

--//工作需要,需要写一个高精度显示事件戳的版本。
--//以前的版本如下:
# cat ts.awk
# /bin/bash
gawk '{ print strftime("[%Y-%m-%d %H:%M:%S]"), $0 }'

--//我看了一下man -a strftime手册发现strftime不支持微秒的显示。只能使用date命令。
--//改写如下:

$ cat tsg.sh
#! /bin/bash
while read i
do
    echo $(date '+%Y/%m/%d %H:%M:%S.%N') : $i
done

--//缺点也是很明显的,就是效率比较低。

 # yes | ts.awk | uniq -c
 290476 [2020-11-23 08:51:14] y
 525268 [2020-11-23 08:51:15] y
 480269 [2020-11-23 08:51:16] y
 548828 [2020-11-23 08:51:17] y
 549501 [2020-11-23 08:51:18] y
^C

--//每秒可以调用50万次。

# yes | tsg.sh | cut -c1-19,30- | uniq -c
    187 2020/11/23 08:54:52 : y
    584 2020/11/23 08:54:53 : y
    591 2020/11/23 08:54:54 : y
    581 2020/11/23 08:54:55 : y
    581 2020/11/23 08:54:56 : y
    596 2020/11/23 08:54:57 : y
    592 2020/11/23 08:54:58 : y
    583 2020/11/23 08:54:59 : y
    580 2020/11/23 08:55:00 : y
^C
--//中间调用cut,也许测试不准。

# yes | tsg.sh > /tmp/aaaa.txt
^C
# cat  /tmp/aaaa.txt  | cut -c1-19,30- | uniq -c
     48 2020/11/23 08:55:38 : y
    575 2020/11/23 08:55:39 : y
    551 2020/11/23 08:55:40 : y
    319 2020/11/23 08:55:41 : y

--//明显我写的版本效率很低。不管先凑合用吧。实际上rh7开始提供ts命令,可以参考链接:
http://blog.itpub.net/267265/viewspace-2643318/

 # ls -l
total 356
-rw-r--r--. 1 root root  77156 2019-05-05 16:16:44 moreutils-0.49-2.el7.x86_64.rpm
-rw-r--r--. 1 root root  39464 2019-05-05 16:27:12 perl-IO-1.25-2.2.x86_64.rpm
-rw-r--r--. 1 root root  42580 2019-05-05 16:31:03 perl-IO-Tty-1.10-11.el7.x86_64.rpm
-rw-r--r--. 1 root root 124524 2019-05-05 16:21:30 perl-IPC-Run-0.92-2.el7.noarch.rpm
-rw-r--r--. 1 root root  52744 2019-05-05 16:23:37 perl-TimeDate-2.30-2.el7.noarch.rpm
-rw-r--r--. 1 root root  16872 2019-05-05 16:21:37 perl-Time-Duration-1.06-17.8.noarch.rpm

# rpm -ivh *.rpm
warning: moreutils-0.49-2.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 9dbd524d: NOKEY
warning: perl-IO-1.25-2.2.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 04b8b50a: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:perl-IO-1.25-2.2                 ################################# [ 17%]
   2:perl-IO-Tty-1.10-11.el7          ################################# [ 33%]
   3:perl-IPC-Run-0.92-2.el7          ################################# [ 50%]
   4:perl-Time-Duration-1.06-17.8     ################################# [ 67%]
   5:perl-TimeDate-1:2.30-2.el7       ################################# [ 83%]
   6:moreutils-0.49-2.el7             ################################# [100%]

# yes | ts | uniq -c
 116491 Nov 23 09:01:53 y
 139789 Nov 23 09:01:54 y
 139343 Nov 23 09:01:55 y
 138720 Nov 23 09:01:56 y
 138508 Nov 23 09:01:57 y
^C
--//可以发现没有awk版本效率高。不过这个ts命令支持高精度时间显示。
# yes | ts  '%Y-%m-%d %H:%M:%.S'  | head
2020-11-23 09:03:54.813767 y
2020-11-23 09:03:54.813868 y
2020-11-23 09:03:54.813880 y
2020-11-23 09:03:54.813890 y
2020-11-23 09:03:54.813912 y
2020-11-23 09:03:54.813920 y
2020-11-23 09:03:54.813929 y
2020-11-23 09:03:54.813937 y
2020-11-23 09:03:54.813963 y
2020-11-23 09:03:54.813973 y


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
卸载Centos下自带mysql
卸载已经安装的rpm包
CentOs/安装XEN配置笔记
利用Rpmforge更新centos?yum源快速安装nagios与cacti(实践)
Redhat Linux通过RPM安装搭建LAMP环境 - Linux - 刘苏平的博客
heartbeat?2.x?安装方式笔记
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服