Link to home
Start Free TrialLog in
Avatar of carol_loveme
carol_loveme

asked on

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

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.

Avatar of carol_loveme
carol_loveme

ASKER

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.
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;
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.
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.
ASKER CERTIFIED SOLUTION
Avatar of dda
dda
Flag of Russian Federation image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks, dda. =)