Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

search a particular file

by raghu_shekar (Novice)
on Mar 10, 2010 at 10:44 UTC ( #827750=perlquestion: print w/replies, xml ) Need Help??
raghu_shekar has asked for the wisdom of the Perl Monks concerning the following question:

hi. This question might sound pretty simple to you guys, hope u can help me out here. I want to go to a directory, list all the files there, check each one of them against a particular name and extension(eg dump*txt), open the file and update it. I have managed mostly everything but im just failing at comparing. <IF $test eq (dump*txt)> Can you please help me out with achieving this.

Replies are listed 'Best First'.
Re: search a particular file
by cdarke (Prior) on Mar 10, 2010 at 12:14 UTC
    I think you are confusing UNIX/Linux shell pattern matching with Perl. Perl uses Extended Regular Expressions for its pattern matching, it only uses shell patterns for filename expansions (globbing). Your if statement should be something like this:
    if ($test =~ /^dump.*txt$/) { ... }
    Notice, lower case if with the condition in parentheses.
    Update: corrected RE
Re: search a particular file
by Anonymous Monk on Mar 10, 2010 at 10:47 UTC
      Hi, thanks for the reply, but i already tried glob. <IF $test eq glob(dump*txt)>. But still the comparision doesnt happen. the array where i store the required value i.e. <@array = $test> inside the IF statement, doesnt give me the reqd result but it goes through all the files in the directory and stores the last file value in the array

        glob already does the matching, according to the patterm you've specified, and returns the filtered filename list. In many cases, there's no need for further testing.  But in case you should want to do so (e.g. to filter by a regular expression, which would allow more sophisticated matching than the shell patterns understood by glob), you could use an additional grep. For example, to get all filenames that start with a substring "foo", which isn't (immediately) followed by "bar":

        #!/usr/bin/perl -l use strict; use warnings; my @files = grep /^foo(?!bar)/, glob("foo*"); print for @files;

        (of course, in this case, using ..., glob("*") would have the same effect, in which case all the filtering would be done by grep alone...)

Re: search a particular file
by rovf (Priest) on Mar 10, 2010 at 11:00 UTC
    In addition to the response you already got, note that the angle operator, when given a glob pattern, returns a list of files matching the pattern

    my @files=<dump*txt>
    Also have a look at File::DosGlob and perlre.

    -- 
    Ronald Fischer <ynnor@mm.st>
Re: search a particular file
by ungalnanban (Pilgrim) on Mar 10, 2010 at 12:30 UTC

    See the following sample code.
    it will match the file name starting with dump and followed by some text and ending with.txt
    #!/usr/bin/perl use strict; use warnings; my @files = qw(dump123.txt abc dump124.txt 123 xyz 12dumpa.txt); foreach (@files) { if ( $_ =~ m/^dump.*txt$/) { print $_."\n"; } } <br>


    --sugumar--
      Hi, Here find the piece of code im using.. pls tell me where im going wrong... i took all your advice and since glob din workout i did the last piece of code. here it is
      #!/usr/bin/perl $path = "path to the directory"; chomp($path); chdir($path); @lis = `ls`; foreach (@lis) { if ($_ =~ m/^dump*txt$/) #say the file i want is dump_234_129_15.txt { print $_."\n"; } }
      here the @lis has all the different files in the directory... but the specific file is not fetched..
        Hi All, I managed to find the mistake i was doing..U guys have been very very helpful.. i missed out the . if ($_ =~ m/^dump.*txt$/) in the if statement. So i guess the script was looking for a file starting with dump and ending with txt. but now its working fine.. Thanks a lot.. you guys rock.. will be back for more... :)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://827750]
Approved by Ratazong
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (9)
As of 2018-06-25 14:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?



    Results (126 votes). Check out past polls.

    Notices?