Solved

How to write a PERL program to read a file with path so that it can file to see if certain files exists ?

Posted on 2002-05-22
6
128 Views
Last Modified: 2010-03-05
Hi experts,

Say now a file called "my_paths.txt".
This file contains some paths
for e.g.

#### part of my_paths.txt ####
my_lib = /fs27/abc.mmm/mylib/
her_cat = /afs/common/hercat/
###############################

These directories are of dirrecent paths.
And in these directories, there are some files.

Now the main purpose of the PERL script is to
(1) read the "my_path.txt" file
(2) look into each path listed in the "my_path.txt"
(3) look for certain file name in each of the path

So, this PERL script will initially prompt 2 inputs
- file names to find (for e.g. happy*.*)
- file name that contains paths (for e.g. my_paths.txt")

So, say in the directory of /fs27/abc.mmm/mylib/ , it contains 4 files, i.e. "happygal.log", "apple.txt", "rem.pl" and "happy23.a"
And in the directory of /afs/common/hercat/, it contains 3 files, i.e. "aaa.zz", "happy77.aaa" and "xxx.m".

Then, by running the script, it will print out 3 file names, i.e. happygal.log , happy23.a and happy77.aaa


Pls kind help to provide this script in full.
Thanks.

0
Comment
Question by:carol_loveme
  • 4
  • 2
6 Comments
 

Author Comment

by:carol_loveme
ID: 7026370
For finding the file names, some frens of mine suggest that I can use something like :-

@file_names = glob ("$input");
print "@file_names \n";

And also for path name matching, probably I can use

/\/w+\b*\/

One thing to mention here is that the path will definitely start with either "/afs/" or "/fs##" where ## can be numbers ranging from 1 to 99.

But the path can be  having 3 or more subdirectory levels,
for e.g.

/afs/sub1/sub2/sub3
/fs33/sub1/sub2/sub3/sub4/sub5/sub6

Pls kindly help to provide a full executable script for this problem.

Thanks.
0
 
LVL 4

Expert Comment

by:dda
ID: 7026441
Try this, I think it is self-explanatory:

#!/usr/bin/perl -w

use strict;

print "Enter file names to find: ";
my $mask = <STDIN>; chomp $mask;

print "Enter file name that contains paths: ";
my $list = <STDIN>; chomp $list;

open LIST, $list or die "Can't open $list file: $!\n";
while (my $path = <LIST>) {
    chomp $path;
    my $fullpath = "$path/$mask";
    print "Looking for [$mask] in [$path]\n";
    foreach (glob $fullpath) {
        print "\tfound: $_\n";
    }
}
close LIST;

exit;
0
 

Author Comment

by:carol_loveme
ID: 7028616
Hi, Sorry, my mistake on this :-

Actually the file "my_path.txt" would contain something like :-
my_lib=/fs27/abc.mmm/mylib/
her_cat=/afs/common/hercat/

in which there will be NO SPACE between "my_lib" and "=" and "/fs27/abc.mmm/mylib" and so on.

So, is there anyway to make the script to actually get the part "/fs27/abc.mmm/mylib" while ignoring "my_lib=" ?

Thanks.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:carol_loveme
ID: 7028619
with the :-
"my_lib=/fs27/abc.mmm/mylib/"
all connected as one line without space in between
the script will take the whole line as one,
hence failed to perform finding correctly.
0
 
LVL 4

Accepted Solution

by:
dda earned 300 total points
ID: 7028961
It's simple:

#!/usr/bin/perl -w

use strict;

print "Enter file names to find: ";
my $mask = <STDIN>; chomp $mask;

print "Enter file name that contains paths: ";
my $list = <STDIN>; chomp $list;

open LIST, $list or die "Can't open $list file: $!\n";
while (my $tmp = <LIST>) {
    chomp $tmp;
    my ($alias, $path) = split /=/, $tmp;
    my $fullpath = "$path/$mask";
    print "Looking for [$mask] in [$alias ($path)]\n";
    foreach (glob $fullpath) {
        print "\tfound: $_\n";
    }
}
close LIST;

exit;
0
 

Author Comment

by:carol_loveme
ID: 7040593
Thanks, dda. =)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Reading fields from the text file. 4 104
Perl efficient DB Call 8 96
syslog unix file 20 78
use google analytics code in perl script 2 90
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question