[SWLUG] locating bits of code in large number of files

Geraint A Edwards gedge-lists at yadn.org
Sun Oct 17 08:53:57 UTC 2004


On Fri, Oct 15, 2004 at 11:14:36PM +0100 or thereabouts, Neil Jones wrote:
> All I really need is a count of the number of files with and without the code 

find . -name \*.html | \
	xargs grep -c getS | \
	awk '	/:0$/{n++;next}
		{m++}
		END{print m,"matching,",n,"non-matching"}'

The find command finds all "*.html" filenames below ".".
The xargs command puts the output of find as arguments to grep,
which counts the number of matching lines in each file.

Then, awk counts "n" for files that do not have the string in
them, (i.e. have a match count of "0") and counts "m" for files
that do (i.e. all other filenames - awk's input lines).
Finally, at the magic "END" line, awk prints the results.


> One complication my hosting , which was chosen 5 years ago before I learned 
> about the wonders of Linux, is on FreeBSD.

Hey!  There's no "complication" for your needs above - Unix-like
O/S's are very similar across flavours for simple things like the
above.  It's only when you need a specific bit of (new or
not-so-popular) hardware or Linux-based bit of software that it
becomes a problem.

I prefer FreeBSD myself (*ducks*), if only because I was using free
Unix-like O/S's years before linux was a (stable) option.  I use
Linux these days on the occasions that people neglect to write
portable Unix-like (and hence distribute Linux-only) code.

I'm now off to put my asbestos jumpsuit on...  ;-p

-- 
Geraint A. Edwards (aka "Gedge") gedge at yadn.org

The three laws (featured in:  I, Republican):
1. A Republican may not injure a corporation, or,
	through inaction, allow a corporation to come to harm.
2. A Republican must obey the orders given it by corporations
	except where such orders would conflict with the First Law.
3. A Republican must protect its own existence as long as such protection
	does not conflict with the First or Second Law.



More information about the Swlug mailing list