Date: Sat, 05 Jun 2010 13:12:42 -0700 From: Bakul Shah <bakul@bitblocks.com> To: freebsd-hackers@freebsd.org Subject: head behaviour Message-ID: <20100605201242.C79345B52@mail.bitblocks.com>
next in thread | raw e-mail | index | archive | help
Consider: $ yes | cat -n | (read a; echo $a; head -1) 1 y 2 y $ yes | cat -n | (head -1; read a; echo $a) 1 y 456 y As you can see, head reads far more than it should. This is fine most of the time but often it results in surprising output: # print ps header and all lines with sh in it $ ps|(head -1; grep sh) PID TT STAT TIME COMMAND # print first and last two lines $ look xa | (head -2; tail -2) xanthaline xanthamic Not quite what you expected, right? Yes, you can use read and echo N times but this is not as convenient as using head: $ look xa | (read a; echo $a; read a; echo $a; tail -2) xanthaline xanthamic xarque Xaverian The "fix" is to make sure head reads no more than $N bytes where $N is the number of *remaining* lines to be read. Yes this slows head down some but makes it more useful. [Ideally all commands that quit after partially reading their input ought to behave like this but that would slow down their common use far too much] Comments? Thanks to Rob Warnock for pointing out the head problem.
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20100605201242.C79345B52>