a question about GREP
Kevin D. Clark
kevin_d_clark at comcast.net
Fri Mar 23 17:01:40 EDT 2007
Jerry writes:
> Find out all plain text files whose file names contain "out" and whose
> contents containing "zip" (in the form of whole word), and then output
> these files names to a file called zip.txt. (These plain text files are
> located in the sub-directories at different levels)
Here is how I would do this:
find your-dirname1 your-dirname2 -name \*out\* \
-exec perl -e 'undef $/;
$filename=$ARGV[0];
$_=<>;
exit(!(-T $filename && /\bzip\b/))' \{\} \; -print \
>zip.txt
Notes:
1: I assume you were serious about the "plain text files" part. This
is what the "-T" bit in the Perl program looks for. No binary
files, right?
2: I assume you were serious about the "zip" part, so a word like
"unzip" would not qualify.
3: The Perl code has some warts, but I was trying for clarity here.
4: The "find" program is very powerful and you can never go wrong
learning about its features.
Regards,
--kevin
PS I thought you might like some of my favorite aliases:
# Author: kevin d. clark
# Finds text files in the specified directories. These use Perl's -T
# and -B tests. Here's some relevant documentation from the perlfunc
# page:
#
# The "-T" and "-B" switches work as follows. The first block or
# so of the file is examined for odd characters such as strange
# control codes or characters with the high bit set. If too many
# strange characters (>30%) are found, it's a "-B" file, other-
# wise it's a "-T" file. Also, any file containing null in the
# first block is considered a binary file. [....] Both "-T" and
# "-B" return true on a null file...
#
# Caveat programmer.
#
# Find text files
txtfind () {
if [ $# -eq 0 ] ; then
txtfind .
else
perl -MFile::Find -e 'find(sub{print "$File::Find::name\n" if (-f
&& -T);}, <at> ARGV);' "${ <at> }"
fi
}
# Find DOS-formatted text files
dostxtfind () {
if [ $# -eq 0 ] ; then
dostxtfind .
else
perl -MFile::Find -e 'find(sub{
$crlf = 0;
if (($f = -f) && ($T = -T)) {
<at> ARGV=($_);
binmode(ARGV);
(/\r\n/ && $crlf++) while(<>);
}
print "$File::Find::name\n"
if ($f && $T && $crlf);
}, <at> ARGV)' "${ <at> }"
fi
}
# Find binary files
binfind () {
if [ $# -eq 0 ] ; then
binfind .
else
perl -MFile::Find -e 'find(sub{print "$File::Find::name\n" if (-f
&& -B);}, <at> ARGV);' "${ <at> }"
fi
}
--
GnuPG ID: B280F24E Never could stand that dog.
alumni.unh.edu!kdc -- Tom Waits
More information about the gnhlug-discuss
mailing list