刷新sed内部的命令 [英] refresh the command inside sed
问题描述
sed 中的date命令不会在每一行上更新.
The date command inside sed
isn't updating on each line.
我正在尝试学习 sed
和其他用于脚本编写的基本命令.在此示例中,我尝试在ping时间之后添加时间戳(这是另一个用户在上一个问题中提供的解决方案),但是 date
命令似乎并没有在每一行上更新.我尝试阅读有关 sed
选项的信息,但到目前为止,我的所有尝试都没有成功
I'm trying to learn sed
and other basic commands for scripting. In this example I'm trying to append a timestamp next to the ping time (a solution provided by another user in a previous question), but the date
command doesn't seem to update on each line. I've tried to read about sed
options, but all my attempts so far have been fruitless
ping 8.8.8.8 |sed --unbuffered"s/.* time = \([0-9 \.] * \).*/\ 1 $(date +%T)/"
我期待
85.2 10:59:26
140 10:59:27
231 10:59:28
97.7 10:59:29
117 10:59:30
80.9 10:59:31
但是我得到
85.2 10:59:26
140 10:59:26
231 10:59:26
97.7 10:59:26
117 10:59:26
80.9 10:59:26
推荐答案
问题是在外壳程序运行 sed
之前,仅对 $(date)
进行一次评估..如果您希望它每次运行,请尝试使用 e
选项代替 sed
.这是 sed
的GNU扩展.
The problem is that the $(date)
is only evaluated once, before the shell runs sed
. If you want it run each time, try the e
option to the sed
substitution. This is a GNU extension to sed
.
$ ping 8.8.8.8 | sed --unbuffered 's/.*time=\([0-9\.]*\).*/echo \1 $(date +%T)/e'
或者如果您想避免 echo
和 $()
:
$ ping 8.8.8.8 | sed --unbuffered 's/.*time=\([0-9\.]*\).*/date "+\1 %T"/e'
这篇关于刷新sed内部的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!