将尾部输出重定向到程序中 [英] Redirecting tail output into a program

查看:72
本文介绍了将尾部输出重定向到程序中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用尾巴作为标准输入向程序发送文本文件中的最新行.

I want to send a program the most recent lines from a text file using tail as stdin.

首先,我向程序回显一些每次相同的输入,然后从应首先通过sed处理的输入文件中发送尾部输入.以下是我期望工作的命令行.但是在程序运行时,它仅接收回声输入,而不接收尾部输入.

First, I echo to the program some input that will be the same every time, then send in tail input from an inputfile which should first be processed through sed. The following is the command line that I expect to work. But when the program runs it only receives the echo input, not the tail input.

(echo "new" && tail -f ~/inputfile 2> /dev/null | sed -n -r 'some regex' && cat) | ./program

但是,以下内容完全可以正常工作,将所有内容打印到终端:

However, the following works exactly as expected, printing everything out to the terminal:

echo "new" && tail -f ~/inputfile 2> /dev/null | sed -n -r 'some regex' && cat

因此,我尝试了另一种类型的输出,并且在发布回显文本的同时,尾部文本没有出现在任何地方:

So I tried with another type of output, and again while the echoed text posted, the tail text does not appear anywhere:

(echo "new"  && tail -f ~/inputfile 2> /dev/null | sed -n -r 'some regex') | tee out.txt 

这使我认为这是缓冲问题,但是我在这里尝试了 unbuffer 程序和所有其他建议(

This made me think it is a problem with buffering, but I tried the unbuffer program and all other advice here (https://superuser.com/questions/59497/writing-tail-f-output-to-another-file) without results. Where is the tail output going and how can I get it to go into my program as expected?

推荐答案

当我为sed命令加上以下前缀时,缓冲问题已解决:

The buffering problem was resolved when I prefixed the sed command with the following:

stdbuf -i0 -o0 -e0 

比使用unbuffer更可取,因为unbuffer对我不起作用.Dave M关于使用sed相对较新的-u的建议似乎也可以解决问题.

Much more preferable to using unbuffer, which didn't even work for me. Dave M's suggestion of using sed's relatively new -u also seems to do the trick.

这篇关于将尾部输出重定向到程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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