Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

bash

shell

tee

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.

like image 845
Bouncner Avatar asked Dec 06 '25 04:12

Bouncner


1 Answers

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 
like image 131
choroba Avatar answered Dec 07 '25 18:12

choroba



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!