grep replacement

Glenn Robitaille glennr at holin.ATT.COM
Wed Jun 1 03:21:16 AEST 1988


> > > There have been times when I wanted a grep that would print out the
> > > first occurrence and then stop.
> > 
> > grep '(your_pattern_here)' | head -1
> 
> Doesn't cut it for
> 
> 	grep '(your_pattern_here)' firstfile secondfile thirdfile ...

Well, if you have a shell command like

	#
	# save the search patern
	#
	patern=$1
	#
	# remove search patern from $*
	#
	shift
	for i in $*
	do
		#
		# grep for search patern
		#
		line=`grep ${patern} ${i}|head -1`
		#
		# if found, print file name and string
		#
		test -n "$line" && echo "${i}:\t${line}"
	done

It'll work fine.  If you want to use other options, have them in
quotes as part of the first argument.


Glenn Robitaille
AT&T, HO 2J-207
ihnp4!holin!glennr
Phone (201) 949-7811



More information about the Comp.unix.wizards mailing list