delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/1999/09/14/15:42:44

Mailing-List: contact cygwin-help AT sourceware DOT cygnus DOT com; run by ezmlm
List-Unsubscribe: <mailto:cygwin-unsubscribe-archive-cygwin=delorie DOT com AT sourceware DOT cygnus DOT com>
List-Subscribe: <mailto:cygwin-subscribe AT sourceware DOT cygnus DOT com>
List-Archive: <http://sourceware.cygnus.com/ml/cygwin/>
List-Post: <mailto:cygwin AT sourceware DOT cygnus DOT com>
List-Help: <mailto:cygwin-help AT sourceware DOT cygnus DOT com>, <http://sourceware.cygnus.com/ml/#faqs>
Sender: cygwin-owner AT sourceware DOT cygnus DOT com
Delivered-To: mailing list cygwin AT sourceware DOT cygnus DOT com
Message-Id: <199909141940.NAA25023@chorus>
Date: Tue, 14 Sep 1999 13:40:53 -0600 (MDT)
From: "13mb80000-HallM(10053584)37x10" <marcus AT bighorn DOT dr DOT lucent DOT com>
Reply-To: "13mb80000-HallM(10053584)37x10" <marcus AT bighorn DOT dr DOT lucent DOT com>
Subject: Re: recursive grep
To: cygwin AT sourceware DOT cygnus DOT com
X-Mailer: dtmail 1.2.1 CDE Version 1.2.1 SunOS 5.6 sun4m sparc
X-Sun-Text-Type: ascii

> 
> Here is a simple rgrep I use, never tried it with bash
> however.
> 
> #!/bin/ksh
> for FILE in `find . -name $2 -print`
> do
> 	if [ `grep -c $1 $FILE` -ne 0 ]
> 	then
> 		echo $FILE
> 		grep -i -n $1 $FILE
> 	fi
> done
> 
> Carson

A much more efficient script here would be:

#!/bin/ksh
pat=$1
shift
find "$@" ! -type d | xargs grep -n -i $pat /dev/null

(Of course, the -n and -i could be left out if that is your preference,
and there could be some attempt to parse them from the script's arguments
instead of building them into the script).

Problems with the original script (for illustration purposes, not meant to
criticise):

	1: The setup for the for loop builds the entire iteration list
	from the output of the find.  If this is really big, some systems
	start to have a problem (because it all has to be in an argument
	vector)
	
	2: The first grep doesn't have the -i, so it requires explicit
	case matching, unlike the 2nd grep (likely a minor oversight).
	
	3: The script starts up one (or two!) grep processes for each and
	every file searched.  This can be costly if there are many files.

One subtle thing about my solution is that the /dev/null argument is passed
to grep.  This is because if grep has only one file argument, it will not
prefix the matched line with the file name.  Giving grep the /dev/null before
whatever arguments xargs tacks on ensures that grep will always have two
file arguments and therefore will always print the file name.

marcus hall

--
Want to unsubscribe from this list?
Send a message to cygwin-unsubscribe AT sourceware DOT cygnus DOT com

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019