[20190502]给显示输出加入时间戳.txt

--//有别人问我执行脚本中timestamp.pl的代码,实际上有些文章里面有源代码,有一些忘记写上了。
--//贴上:
$ cat /usr/local/bin/timestamp.pl
#!/usr/bin/perl
while (<>) {
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
printf("%02d:%02d:%02d", $hour, $min, $sec);
print  ": $_";
#print localtime() . ": $_";
}

--//使用timestamp.pl在开始标注时间.这样更加清晰.我好像改过,实际上这个很容易自己写一个。

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

--//这是我临时想到的脚本,看了链接:https://serverfault.com/questions/310098/how-to-add-a-timestamp-to-bash-script-log
--//真心佩服老外,人家还考虑执行效率.
gawk '{ print strftime("[%Y-%m-%d %H:%M:%S]"), $0 }'
while true; do printf '%(%F %T)T\n'; done

--//顺便在我的笔记本上测试看看.我使用Cygwin64 Terminal for windows:
$  yes | gawk '{ print strftime("[%Y-%m-%d %H:%M:%S]"), $0 }' | uniq -c
 180224 [2019-05-03 20:23:53] y
 433126 [2019-05-03 20:23:54] y
 430354 [2019-05-03 20:23:55] y
 430532 [2019-05-03 20:23:56] y
 428690 [2019-05-03 20:23:57] y
 432775 [2019-05-03 20:23:58] y

$ yes |while read i; do printf '%(%F %T)T';echo " $i" ; done | uniq -c
   1406 2019-05-03 20:29:13 y
  12101 2019-05-03 20:29:14 y
  12080 2019-05-03 20:29:15 y
  12111 2019-05-03 20:29:16 y
  12048 2019-05-03 20:29:17 y
  12373 2019-05-03 20:29:18 y
  12350 2019-05-03 20:29:19 y

$ yes |while read i; do echo $(date '+%H:%M:%S') " $i"; done | uniq -c
      6 20:32:29  y
     33 20:32:30  y
     31 20:32:31  y
     30 20:32:32  y
     31 20:32:33  y
     33 20:32:34  y
     33 20:32:35  y
     33 20:32:36  y

$ yes | xargs -I{} date "+%H:%M:%S : {}" | uniq -c
     31 20:45:22 : y
     31 20:45:23 : y
     35 20:45:24 : y
     34 20:45:25 : y
     35 20:45:26 : y
     35 20:45:27 : y
     34 20:45:28 : y

--//实际上还有1个现成的ts命令(我没有找到,不知道在那个rpm包里面)以及perl脚本的情况.上班测试看看.

$ yes | timestamp.pl | uniq -c
 267209 08:56:02: y
 308591 08:56:03: y
 308820 08:56:04: y
 308579 08:56:05: y
 308996 08:56:06: y
 282290 08:56:07: y
 304223 08:56:08: y

$ yes | gawk '{ print strftime("[%Y-%m-%d %H:%M:%S]"), $0 }' | uniq -c
 190537 [2019-05-05 08:56:58] y
 516917 [2019-05-05 08:56:59] y
 518052 [2019-05-05 08:57:00] y
 517918 [2019-05-05 08:57:01] y
 518543 [2019-05-05 08:57:02] y
 517913 [2019-05-05 08:57:03] y

--//$ yes |while read i; do printf '%(%F %T)T';echo " $i" ; done | uniq -c在我的linux 5.9不支持.在rhel7 测试,显示的时间是:
1970-01-01 08:00:00 y
1970-01-01 08:00:00 y
--//有问题.

$ yes |while read i; do echo $(date '+%H:%M:%S') " $i"; done | uniq -c
    210 09:00:26  y
    435 09:00:27  y
    433 09:00:28  y
    438 09:00:29  y
    439 09:00:30  y

$ yes | xargs -I{} date "+%H:%M:%S : {}" | uniq -c
    223 09:00:49 : y
    803 09:00:50 : y
    803 09:00:51 : y
    798 09:00:52 : y
   1018 09:00:53 : y
    814 09:00:54 : y
    812 09:00:55 : y

--//google还找到如下链接:
https://unix.stackexchange.com/questions/26728/prepending-a-timestamp-to-each-line-of-output-from-a-command

Firstly, if you are expecting these timestamps to actually represent an event, bear in mind that since many programs
perform line buffering (some more aggressively than others), it is important to think of this as close to the time that
the original line would have been printed rather than a timestamp of an action taking place.

You may also want to check that your command doesn't already have an inbuilt feature dedicated to doing this. As an
example, ping -D exists in some ping versions, and prints the time since the Unix epoch before each line. If your
command does not contain its own method, however, there are a few methods and tools that can be employed, amongst
others:

POSIX shell

Bear in mind that since many shells store their strings internally as cstrings, if the input contains the null character
(\0), it may cause the line to end prematurely.

command | while IFS= read -r line; do printf '[%s] %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$line"; done

GNU awk

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

Perl

command | perl -pe 'use POSIX strftime; print strftime "[%Y-%m-%d %H:%M:%S] ", localtime'

Python

command | python -c 'import sys,time;sys.stdout.write("".join(( " ".join((time.strftime("[%Y-%m-%d %H:%M:%S]", time.localtime()), line)) for line in sys.stdin )))'

Ruby

command | ruby -pe 'print Time.now.strftime("[%Y-%m-%d %H:%M:%S] ")'

--//Python,ruby(没安装)没有测试,其它测试如下:

$ yes | perl -pe 'use POSIX strftime; print strftime "[%Y-%m-%d %H:%M:%S] ", localtime'| uniq -c
  10259 [2019-05-05 09:02:30] y
 140363 [2019-05-05 09:02:31] y
 144397 [2019-05-05 09:02:32] y
 144285 [2019-05-05 09:02:33] y
 131107 [2019-05-05 09:02:34] y

$ yes | while IFS= read -r line; do printf '[%s] %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$line"; done|uniq -c
     50 [2019-05-05 09:04:09] y
    410 [2019-05-05 09:04:10] y
    400 [2019-05-05 09:04:11] y
    400 [2019-05-05 09:04:12] y

--//从测试看awk脚本效率最高,使用这个脚本主要目的看某些步骤的执行时间间隔.例子:

$ ping -c 3 -i 2 192.168.100.40 | ts.awk
[2019-05-05 09:08:36] PING 192.168.100.40 (192.168.100.40) 56(84) bytes of data.
[2019-05-05 09:08:36] 64 bytes from 192.168.100.40: icmp_seq=1 ttl=64 time=0.855 ms
[2019-05-05 09:08:38] 64 bytes from 192.168.100.40: icmp_seq=2 ttl=64 time=0.094 ms
[2019-05-05 09:08:40] 64 bytes from 192.168.100.40: icmp_seq=3 ttl=64 time=0.165 ms
[2019-05-05 09:08:40]
[2019-05-05 09:08:40] --- 192.168.100.40 ping statistics ---
[2019-05-05 09:08:40] 3 packets transmitted, 3 received, 0% packet loss, time 4000ms
[2019-05-05 09:08:40] rtt min/avg/max/mdev = 0.094/0.371/0.855/0.343 ms

--//顺便问一下,那位知道ts这个命令在那个rpm安装包里面.那位知道?遇到这样的情况如何查询确定安装包.

[20190502]给显示输出加入时间戳.txt的更多相关文章

  1. 把cmd信息中的正常和异常输出分别输出到不同txt文件中

    场景一: 1.大量滚动信息容纳不下,在小黑屏中被冲刷掉. 2.希望把正常输出和异常输出分别输出到不同地方. 相关命令 一共有4个输出到文件的命令,现以jar命令打war包举例说明: 命令 说明 举例  ...

  2. 【matlab】将matlab中数据输出保存为txt或dat格式

    将matlab中数据输出保存为txt或dat格式 总结网上各大论坛,主要有三种方法. 第一种方法:save(最简单基本的) 具体的命令是:用save *.txt -ascii x x为变量 *.txt ...

  3. Linux 命令 - watch: 反复执行命令,全屏显示输出

    watch 命令周期性地执行命令,全屏显示输出.可以通过 watch 命令反复执行某一程序来监视它的输出变化. 命令格式 watch [-dhvt] [-n <seconds>] [--d ...

  4. YUV422蓝屏显示输出功能辅助调试

    YUV422蓝屏显示输出功能辅助调试 YUV422有YUYV,YVYU,UYVY,VYUY四种,以下笔者就就以UYVY为例介绍一下数据构成.因为常常要跟视频输入打交道,所以YUV422这种常见的视频信 ...

  5. MATLAB 显示输出数据的三种方式

    MATLAB 显示输出数据的三种方式 ,转载 https://blog.csdn.net/qq_35318838/article/details/78780412 1.改变数据格式 当数据重复再命令行 ...

  6. [20171220]toad plsql显示整形的bug.txt

    [20171220]toad plsql显示整形的bug.txt --//下午有itpub网友反应,一个查询在sqlplus,pl/sql下不同.链接如下:--//http://www.itpub.n ...

  7. 【Linux开发】将cmd中命令输出保存为TXT文本文件

    将cmd中命令输出保存为TXT文本文件 在网上看到一篇名为:"[转载]如何将cmd中命令输出保存为TXT文本文件" 例如:将Ping命令的加长包输出到D盘的ping.txt文本文件 ...

  8. 查找默认安装的python路径,并输出到 FindPythonPathX_output.txt

    在python程序设计教学中,在汉化IDEL时.为PyCharm项目设置解释器时,经常需要查找python安装路径.对老手来说很简单,但对很多刚开始学习编程的学生来说,则很困难.所以,编写了一个批处理 ...

  9. 【Linux】将终端的命令输出保存为txt文本文件

    Linux中的终端很方便,可以直接复制粘贴的. 之后开一个gedit文本编辑器,把复制到的内容粘贴就可以的. 不像windows的cmd控制台,需要先右键标题栏,选择编辑->全选/标记,在右键标 ...

随机推荐

  1. Spring Boot2.1.7启动zipkin-server报错:Error creating bean with name 'armeriaServer' defined in class path

    修改项目,更新组件版本时,引入了最新版本2.12.9的zipkin-server和zipkin-autoconfigure-ui时,服务启动报错: org.springframework.beans. ...

  2. docker容器跨服务器的迁移的方法

    docker的备份方式有export和save两种. export是当前的状态,针对的是容器,docker save 是针对镜像images. export 找出要备份容器的ID ? 1 2 3 [r ...

  3. tcp滑动窗口和读写缓冲区

    最近突然忘记了 滑动窗口的原理,在网上找到了比较好的视频,现在在这里同大家分享: 注:反正进程间切换 视频链接: https://www.youtube.com/watch?v=R6ArbkVj-N8 ...

  4. 6. Vue - 声明周期

    一.官方vue生命周期流程图 二.vue声明周期介绍 beforeCreate执行时:data和el均未初始化,值为undefined created执行时:Vue 实例观察的数据对象data已经配置 ...

  5. 【转】gdb的调试与使用

    转载自:https://www.jianshu.com/p/7a06b0bda2d8 gdb的调试与使用 这篇应该是我见过的总结最详细的gdb调试指南了,这位博主是个很强的人,他的博客对萌新比较友好, ...

  6. python3.5.3rc1学习十:网络请求

    #sys模块import sys sys.stderr.write('This is stderr text\n')# 因为从定向有缓冲区,所以需要以下这行代码sys.stderr.flush()sy ...

  7. vue中的router和route有什么区别?

    我只知道前者一般用在跳转路由的时候,push一个url, 而后者则用来存储路由跳转过程中存储的各种数据. 话不多说,这篇博客讲的比较详细,可以参考一下. vue2.0中的$router 和 $rout ...

  8. php 学习笔记之搭建开发环境(mac版)

    Mac 系统默认集成了很多开发工具,其中就包括 php 所需要的一些软件工具. 下面我们将搭建最简单的 php 开发环境,每一步都会验证上一步的操作结构,请一步一步跟我一起搭建吧! web 服务器之 ...

  9. Django signal(信号)

    Django中提供了"信号调度",用于在框架执行操作时解耦,就是一些动作发生的时候,信号允许特定的发送者去提醒一些接受者. Django 内置信号 Model signals pr ...

  10. Redis与python

    一.Redis介绍 Redis是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库(非关系型数据库). 本质:将数据保存在内存中. 用途:缓存.消息队列. 1.Redis的特点 R ...