Parse file names for string and then grep these files ??

Parse file names for string and then grep these files ??

Post by adrienne_w » Thu, 18 Sep 2003 01:42:00


Help,
awk, sed, find, grep - How to combine these commands (or others) to
solve my issue?

I need to gather a list of files from 2 directories with a common
string in the filename (for instance I would be looking for files with
todays date and yesterdays present in the filename - like
/home/myDir/myFile.20030916.out and
/home/myOtherDir/myFile.20030915.out)

I borrowed ydate from unix.about.com to construct these date strings.

Once I have this list of files, I need to grep for a particular string
and write these lines to a new output file.
Maybe perl is really what I should be looking at, my book says read is
not good in unix script files.

Thanks for any help !!
 
 
 

Parse file names for string and then grep these files ??

Post by Andreas Ka » Thu, 18 Sep 2003 01:48:30


Assuming a sh-derived shell (I'm using ksh):

find /home -type f -name "myFile*" -print |
while read filename; do
grep 'pattern' $filename
done

or

find /home -type f -name "myFile*" -exec grep 'pattern' /dev/null {} \;

or

find /home -type f -name "myFile*" -print | xargs grep 'pattern' /dev/null


That last alternative is probably the fastest. The first one is
suitable if you need to do more with the filename or the fact
that it grep succeeds or fails.


--
Andreas Ki

 
 
 

Parse file names for string and then grep these files ??

Post by John W. Kr » Thu, 18 Sep 2003 06:41:18


Yes, you can do this in perl.

#!/usr/bin/perl -w

my ( $day, $mon, $yr ) = ( localtime )[ 3, 4, 5 ];
my $today = sprintf '%04d%02d%02d', $yr + 1900, $mon + 1, $day;
( $day, $mon, $yr ) = ( localtime time - 86400 )[ 3, 4, 5 ];
my $yesterday = sprintf '%04d%02d%02d', $yr + 1900, $mon + 1, $day;

@ARGV = </home/myDir/*$today* /home/myOtherDir/*$yesterday*>;

open OUT, '> output.file' or die "Cannot open output.file: $!";

while ( <> ) {
print OUT if /a particular string/;
}

__END__



John
--
use Perl;
program
fulfillment
 
 
 

Parse file names for string and then grep these files ??

Post by Andreas Ka » Thu, 18 Sep 2003 06:59:50

In article <slrnbmefms.cgh.ak+ XXXX@XXXXX.COM >,



Sorry, I missed the part about the date string.

Assuming you have the correct date string in the variable
$thedate, just replace "myFile*" in my previous response (below)
with "myFile.${thedate}.out".

Also, you might want to do one 'find' invocation on each
subdirectory instead of on /home as I said first, especially
if there are many subdirectories of /home that you're not
interested in.


Notice how this also may be written

while read filename; do
grep 'pattern' $filename
done <filelist

... if you already have a list of files in 'filelist'.


[cut]



--
Andreas Ki
 
 
 

Parse file names for string and then grep these files ??

Post by adrienne_w » Fri, 19 Sep 2003 02:47:14


Wow. Thank you both. I'm going to try both suggestions. Hopefully
I'll get better at this.