Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating an advance search perl script

Status
Not open for further replies.

skw

Instructor
Jun 1, 1999
19
US
I did a perl script that searches for files, but I want to enhance this perl script so that it does a more indepth/advance search.

E.g., I have log files, and the log file names are typically, e.g., LogFileName2.0.0.20030715.box1.csv. I did a search script that'll find any log file, but I want it to also search by the boxname, the version and the date.

In the script I did, I have this. Can someone explain how to setup this up for an advance search. Thanks.

### GET FILES

$itec = 0;

foreach $item (@searchcrit)
{

if (-e "$item")
{
opendir(DIR,"$item");
@files = readdir(DIR);
closedir (DIR);

$fnc = 0;
foreach $fn (@files)
{
$files[$fnc] = "$item/$fn" . "\t" . "$weburls[$itec]/$fn";
$fnc++;
}

}
else
{
print "Error - $item does not exist.";
}

$itec++;
$tmpc = push(@allfiles,@files);
}


### SORT FILES

$sc = 0;

foreach $item (@allfiles)
{
foreach $ext (@searchext)
{
if ($ext eq substr($item, length($item) - length($ext), length($ext)))
{
$sfiles[$sc] = $item;
$sc++;
}
}
}
 
skw,

There's a very good indexing script, and search script available free from and the correct installtion of that would suit your needs easily, down to searching the contents of the files, as well as file names.

However, if you want to write this all yourself, and I appreciate the need to do it, I'd like to point to some possible inefficiencies that will run against you with this method.

1) You're not ranking your searches (ie. if you've got a few criteria, the best matches aren't being promoted to the top of the list)

2) you're using -e, as opposed to -d for directories, which could potentially mean opening a file as a directory, which I'm not sure you want

3) It might be just me, but it looks like in the event of a match, your adding the entire @files array to @allfiles.

4) For each search crieria entered you're looking for an exact match (if (-e $item) {), that will only allow an exact match, and the it will try to open the directory

I'm after writing this without the context of your total script, or your exact specs, which might make things clearer.



HTH
--Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
Try File::Find::Rule [1]....

[1]
my @logs = File::Find::Rule->file()->name( qr/$search/ )->in(@dirs);

where $search is your search pattern, eg 'LogFileName2.0.0' or '20030715' or 'box1' or even '\.csv$', and @dirs contains a list of the directories to search, which can just be one directory. Its recursive too. Returns the full path of all the log files it finds.

Barbie
Leader of Birmingham Perl Mongers
 
Paul, thanks for responding.... I think I may have tried Perlfect but it was just too complicated to install and I got frustrated with it ...

<<There's a very good indexing script, and search script available free from and the correct installtion of that would suit your needs easily, down to searching the contents of the files, as well as file names.>>

skw- that's the type of search I want, but I'd prefer not use an indexing script.


<<However, if you want to write this all yourself, and I appreciate the need to do it, I'd like to point to some possible inefficiencies that will run against you with this method.>>


<<1) You're not ranking your searches (ie. if you've got a few criteria, the best matches aren't being promoted to the top of the list)>>

skw-- it's not important to rank the search, but I want to search for best matches. That's if I enter a logfilename &quot;MyLog&quot;, enter a date 20030101, a version [2.2.0] find all MyLog2.0.0 dated 01/01/2003 -- what makes this search unique is the filename since each string I'm searching for is the complete string of the file name.

<<<2) you're using -e, as opposed to -d for directories, which could potentially mean opening a file as a directory, which I'm not sure you want>>>

skw- I guess I used -e because I have an @searchdirs (&quot;list/dirs/to/search/in&quot;, &quot;dirs/to/search/in&quot;), so I wanted it to be &quot;true if the file name exists&quot; ... I don't know if what I just said makes sense... I'm still virtually new to perl and teaching myself :-(

<<3) It might be just me, but it looks like in the event of a match, your adding the entire @files array to @allfiles.>>

skw-- I think I am doing that *but* I'm not entirely sure if that's what I want in the end results.

<<<4) For each search crieria entered you're looking for an exact match (if (-e $item) {), that will only allow an exact match, and the it will try to open the directory

I'm after writing this without the context of your total script, or your exact specs, which might make things clearer.>>>

skw-- I'll post the script here -- and it's kinda long

==========

missbarbell, I understand what you're saying, but I guess I'm not getting how to get it done. I'll read the info on cspan about File::Find::Rule and see how I can use it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top