bash - Misbehaving head with redirection -


in reply piping file through tail , head via tee, strange behaviour of head has been observed in following construct when working huge files:

#! /bin/bash in {1..1000000} ; echo $i ; done > /tmp/n  ( tee >(sed -n '1,3p'        >&3 ) < /tmp/n | tail -n2 ) 3>&1 # correct ( tee >(tac | tail -n3 | tac >&3 ) < /tmp/n | tail -n2 ) 3>&1 # correct ( tee >(head -n3             >&3 ) < /tmp/n | tail -n2 ) 3>&1 # not correct!? 

output:

1 2 3 999999 1000000 1 2 3 999999 1000000 1 2 3 15504 15 

question:

why not last line output same lines previous 2 lines?

this because head exits transfers 3 first lines. subsequently, tee gets killed sigpipe because reading end of "file" pipe writing closed, not until manages output lines stdout.

if execute this:

tee >(head -n3 >/dev/null) < /tmp/n 

you see happens better.

otoh, tac reads whole file has reverse it, sed, consistent.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -