Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

shell to perl question

by tech2040 (Novice)
on Aug 18, 2005 at 07:38 UTC ( [id://484691]=perlquestion: print w/replies, xml ) Need Help??

tech2040 has asked for the wisdom of the Perl Monks concerning the following question:

Greetings all,
I have a quick question.

I am working on a few prototype functions and need some help. Using UNIX, I am usine the following script to sort image files:

find ./Images | egrep '\.jpe?g$'

1. Is there any way to use this script in perl? (I have alredy tried "system")
2. Is there a perl based alternative?

Any help will be greatly appreciated.

Formatting and code tags added by davido, per consideration.

Replies are listed 'Best First'.
Re: shell to perl question
by b10m (Vicar) on Aug 18, 2005 at 07:41 UTC

    File::Find ?

    You also might like the program find2perl that comes with that module.

    --
    b10m

    All code is usually tested, but rarely trusted.
      An example
      use File::Find; find (\&wanted, @ARGV); sub wanted { return unless /\.jpe?g$/i; print "Found $_\n"; }

        You probably want

        print "Found $File::Find::name\n";
        there, otherwise only the basename gets printed out.

        the lowliest monk

Re: shell to perl question
by salva (Canon) on Aug 18, 2005 at 08:15 UTC
    you can use the backticks to capture the output from some command:
    @files = `find ./Images | egrep '\.jpe?g$'`; chomp(@files);
    or
    @files = grep /\.jpe?g$/i, `find ./Images`; chomp(@files);
      In case you're wondering exactly how that works, perl is implicitly splitting the output on a newline character, because it's being assigned to an array. The following is an equivalent, explicit version of what's going on.
      my $files = `find ./Images | egrep '\.jpe?g$'`; my @files = split(/\n/, $files);
      UPDATE: My split was slightly off, as corrected below. Who does this guy think he is, correcting my code? ;-)
      ---
      my name's not Keith, and I'm not reasonable.
        Close. Actually, it's equivalent to
        my @files = split(/^/, $files);
        since it leaves the newlines on each indivdual list element.
Re: shell to perl question
by merlyn (Sage) on Aug 18, 2005 at 13:59 UTC
Re: shell to perl question
by zentara (Archbishop) on Aug 18, 2005 at 13:48 UTC
    And don't forget about a file "glob"
    #!/usr/bin/perl use warnings; use strict; my @files = <*.jpg *.jpeg somedir/*.jpg>; my @sorted = sort @files; print "@sorted\n";

    I'm not really a human, but I play one on earth. flash japh

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://484691]
Approved by aukjan
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-03-28 22:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found