Problem using multiple 'head' commands in shell script

Darin McGrew mcgrew at ichthous.Eng.Sun.COM
Wed Jan 30 12:52:08 AEST 1991


In article <1671 at abekrd.UUCP> garyb at abekrd.UUCP (Gary Bartlett) writes:
->Can someone explain to me what is happening with the following Bourne shell
->script and more importantly how I can get around it:
->
->	#!/bin/sh
->	cat file | (
->		head -200
->		echo "Line 201 follows"
->		head -200
->		echo "Line 401 follows"
->		cat
->	)
->
->...
->It looks like 'head' initially reads in a whole buffer of data from file
->(stdin), prints out the requisite number of lines and then dumps the rest
->of the buffer.  The next 'head' then reads the NEXT buffer....

Yes, head reads a bufferful at a time.  I'd use awk:

	awk '	NR==201	{print "Line 201 follows"}
		NR==401	{print "Line 401 follows"}
			{print}' < file

->Thanks,
->Gary

You're welcome.

                 Darin McGrew     "The Beginning will make all things new,
           mcgrew at Eng.Sun.COM      New life belongs to Him.
       Affiliation stated for      He hands us each new moment saying,
identification purposes only.      'My child, begin again....
				    You're free to start again.'"



More information about the Comp.unix.shell mailing list