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

Hey Monks,

I would like to do the following things:

1. Navigate to a certain directory: (/rjo/fnl/logfiles/)

2. Open all files with the name: (*RAN*.log)

3. Run prepared script on each file.

4. Within that script, create and output to a new file: (original filename.pd)

If anyone could show me an example of how to do this it would be awesome. Otherwise, if anyone can point me in the right direction for literature on how to set this up it would be much appreciated. Thanks!

  • Comment on Opening files from a directory and batch processing into new files

Replies are listed 'Best First'.
Re: Opening files from a directory and batch processing into new files
by thenaz (Beadle) on Aug 03, 2011 at 21:01 UTC

    If you are running this on a shell (bash or ksh), you could simply do:

    for f in *RAN*.log; do ./script.pl < $f > $f.pd; done

    where script.pl might look like:

    #!/usr/bin/perl while (<STDIN>) { # do something print "output\n"; }

      That would be so much more simple! Alas, my supervisor has requested I do it in perl.:/ Thanks for the advice though!

Re: Opening files from a directory and batch processing into new files
by toolic (Bishop) on Aug 03, 2011 at 20:59 UTC
    1. chdir
    2. glob... it seems like you only need a list of files here, instead of opening each file
    3. system to run a script
    4. open
      Thanks for pointing me in the right direction, those thinks have been endlessly helpful!
Re: Opening files from a directory and batch processing into new files
by i5513 (Pilgrim) on Aug 03, 2011 at 21:03 UTC
    I would do that in bash (which is perfect to execute commands)
    for f in /path/to/files/*RAN*.log do script "$f" > "$f".pd done
    If you want to do that in perl:
    See glob for globbing, and system for executing and maybe perlsyn for foreach syntax
    Regards, Updated to double quote vars (thanks to cdarke)