Redirecting tail output to a program

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

First, I echo the program with some input that will be the same every time, and then send the tail input from the input file, which must first be processed through sed. Below is the command line that I expect to work. But when the program runs, it receives only the echo input, not the tail input.

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

However, it works as expected, printing everything to the terminal:

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

So, I tried with a different type of output, and again, when the text of the echo message is posted, the tail text is not displayed anywhere:

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

This made me think that this is a buffering problem, but I tried the program unbufferand all the other recommendations here ( https://superuser.com/questions/59497/writing-tail-f-output-to-another-file ) without any either results. Where does the tail exit and how can I get it to enter my program as expected?

+4
source share
3 answers

The buffering problem was resolved when I prefixed the sed command as follows:

stdbuf -i0 -o0 -e0 

It is much preferable to use unbuffer, which did not even work for me. Dave M suggests using sed relatively new - it also seems to do the trick.

+1
source

, : | () , && ( ). ,

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

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

, cat , sed, , . -u sed, :

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

, sed -u, , , , .

+1

You can use the command iin sed(see the list of commands in the man page for details) to perform an insert at the beginning:

tail -f inputfile | sed -e '1inew file' -e 's/this/that/' | ./program
0
source

All Articles