[geeks] Way to do this in AWK?

Greg A. Woods woods at weird.com
Fri Jun 21 12:35:39 CDT 2002


[ On Thursday, June 20, 2002 at 23:48:27 (-0400), alex j avriette wrote: ]
> Subject: Re: [geeks] Way to do this in AWK?
>
> > do is take a file and prepend it to another file,
> > then trim the file to 15 lines.
> 
> erm, you could use head?

Once upon a time 'head' was the least portable way to get the first few
lines of a file.  'Sed' usually stands in for 'head' (though it's by far
not the only way to do similar tasks), and lots of us with old porting
nightmares still use it.

The most efficient way to use sed, with all the variants I've
encountered, is this expression, expressed here as the last statement in
a shell function almost equivalent to the implementation of the original
BSD 'head' command:

	head ()
	{
		N=10
		if [ $# -ge 1 ] ; then
			case "$1" in
			-[0-9]*)
				N=`expr x"$1" : '^x-\(.*\)$'`
				shift
				;;
			-*)
				echo "Usage: head [-N] [[file] ...]" 1>&2
				return 2
			esac
		fi
		sed ${N}q ${1+$@}
	}

(all machines without 'head' had a shell with functions)

-- 
								Greg A. Woods

+1 416 218-0098;  <gwoods at acm.org>;  <g.a.woods at ieee.org>;  <woods at robohack.ca>
Planix, Inc. <woods at planix.com>; VE3TCP; Secrets of the Weird <woods at weird.com>



More information about the geeks mailing list