01-28-04 07:35 PM
On Wed, 28 Jan 2004 10:36:40 -0800, Garbunkel wrote:
quote:
> Okay. I've only been doing the script thing for a couple of weeks
> now, so please go easy on me...
>
> I'm attempting to execute a program which will input from the user a
> directory path, file extension, and a series of variables (user is
> prompted for variables until they input that they're done). The
> command that I want executed (one time for each variable input is:
> 'find <pathname> <extension> -exec grep -i <variablename> {} \;
>
> The reason that I don't just do a simple grep is that there are too
> many files in the target directory with the same file extension (I get
> an error message if I try a simple grep).
>
> Any help with how to pull this off would be much apprec. This is what
> I have so far...
[snipped]
Difficult to know where to start with this one. One thing which makes a
big difference is if the things you are searching for are fixed strings
or regular expressons. If they are fixed strings then something like
------------------------------------
#!/bin/sh
echo "Enter path to search from"
read dir
echo "Enter strings to search for, one per line, type CTRL-D to end"
filename=/tmp/findpat$$
cat > $filename
echo "Enter extension of files to search, including '.'"
read ext
find ${dir-.} -type f -name "*$ext" -print | xargs fgrep -f $filename /dev/n
ull
rm $filename
------------------------------------
would do it. Of course it could be that you are trying to make a tool that
finds all uses of a particular variable name in a set of files. If so there
are other tools you can look at, the GNU 'id' tools are one example.
If you need to have the regular expressions, then you can build up the
pattern to search for, and then apply it all at once. Some versions of
grep (in particular GNU grep) have a '-f' option so you can replace
the fgrep with grep in the above example.
It may be worth while building up the list of files to search once, e.g.
--------------------------------------------------
#!/bin/sh
echo "Enter path to search from"
read dir
echo "Enter extension of files to search, including '.'"
read ext
filename=/tmp/findfiles$$
find ${dir-.} -type f -name "*$ext" -print > $filename
echo "Enter strings to search for, one per line, type CTRL-D to end"
while read pattern
do
xargs grep -i "$pattern" /dev/null < $filename
done
rm $filename
--------------------------------------------------
This is particularly true if you have a deep tree to search, but you imply
you are only interested in a particular directory. If that is the case, then
the following will work for the current directory
--------------------------------------------------
#!/bin/sh
echo "Enter extension of files to search, including '.'"
read ext
echo "Enter strings to search for, one per line, type CTRL-D to end"
while read pattern
do
ls -f | grep "$ext\$" |xargs grep -i "$pattern" /dev/null
done
--------------------------------------------------
Sorry, all of the above are untested.
[ Post a follow-up to this message ]
|