Piping a file through tail and head via tee [closed]

倖福魔咒の 提交于 2019-12-10 20:12:50

问题


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 


来源:https://stackoverflow.com/questions/16657209/piping-a-file-through-tail-and-head-via-tee

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!