Piping a file through tail and head via tee [close

2019-07-05 13:39发布

问题:

Starting from here I tried to read a file and emit the head and the tail of the file (reading the file only once).

I tried the following: tee >(head) >(tail) > /dev/null < text.txt

This line works as expected, but I'd like to get rid of the /dev/null. So I tried: tee >(head) | tail < text.txt

But this line does not work as expected (well, as I expected), it prints the head but does not return after that. Apparently tail is waiting for something. But I don't know what for exactly. I found this SO question, but I could not get it running with the given answers.

回答1:

In tee >(head) | tail < text.txt, the text file goes directly to tail. You probably meant

tee >(head) < text.txt | tail

Which does not wait for anything, but does not work either, because the output of both tee and head go to tail.

Redirecting the head's output to a new file descriptor and then taking it back works, but I am not sure it is "cleaner" than using /dev/null:

( tee >(head >&3) < text.txt | tail) 3>&1 


标签: bash shell tee