做一个尾巴-F直到匹配模式 [英] Do a tail -F until matching a pattern

查看:102
本文介绍了做一个尾巴-F直到匹配模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对一个文件尾巴-F直到模式匹配的。我发现用awk的一种方式,但恕我直言我的命令是不是真的干净。问题是,我的需要以做到在只有一条线,因为有些限制。

I want to do a tail -F on a file until matching a pattern. I found a way using awk, but IMHO my command is not really clean. The problem is that I need to do it in only one line, because of some limitations.

tail -n +0 -F /tmp/foo | \
awk -W interactive '{if ($1 == "EOF") exit; print} END {system("echo EOF >> /tmp/foo")}'

尾将阻塞,直到EOF出现在文件中。它的工作原理pretty好。结束块是强制性的,因为awk的退出不退出的时候了。它使AWK退出前EVAL END块。 END块挂在读取调用(因为尾部的),所以我需要做的最后一件事,就是写另一行的文件,以迫使尾要退出

The tail will block until EOF appears in the file. It works pretty well. The END block is mandatory because awk's "exit" does not exit right away. It makes awk to eval the END block before quitting. The END block hangs on a read call (because of tail), so the last thing I need to do, is to write another line in the file to force tail to exit.

是否有人知道一个更好的方式来做到这一点?

Does someone know a better way to do that?

推荐答案

试试这个:

sh -c 'tail -n +0 -f /tmp/foo | { sed "/EOF/ q" && kill $$ ;}'

整个命令行将会尽快EOF字符串在/ tmp / foo的看到退出。

The whole command-line will exit as soon as the "EOF" string is seen in /tmp/foo.

有一个副作用:尾进程将一直运行(在后台),直到事情被写入到/ tmp / foo的

There is one side-effect: the tail process will be left running (in the background) until anything is written to /tmp/foo.

这篇关于做一个尾巴-F直到匹配模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆