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
124 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

705 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now