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
129 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

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!

Question has a verified solution.

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

Suggested Solutions

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

739 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