Perl Script for Parsing

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • v796

    Perl Script for Parsing

    Hi,

    I am looking out for a Perl script to do the following. I am quite
    positive someone must have written something similar.

    - A perl script to open a directory/folder and access all files and
    directories(and files within it)after a particular time stamp.

    - Then for each file with a particular string in its name, access the
    file and look for string
    ':20C::RELA//49879'

    - Based upon that do throw an error or proceed for some other tasks.


    I don't have the details yet from my manager, but I figure this script
    will be run as a cron - every night.
    I know PERL is pretty nifty for such tasks, that's why I feel
    confident that someone must have written a script
    already.

    I did look at CPAN, but it is overwhelming. Still looking though.

    Thanks,
    - v796.
  • Jürgen Exner

    #2
    Re: Perl Script for Parsing

    v796 wrote:[color=blue]
    > I am looking out for a Perl script to do the following. I am quite
    > positive someone must have written something similar.
    >
    > - A perl script to open a directory/folder[/color]

    perldoc -f opendir
    perldoc -f readdir
    [color=blue]
    > and access all files and directories[/color]

    perldoc perlsyn (in particular the for and while loops)
    perldoc -f open
    [color=blue]
    > (and files within it)[/color]

    Ah, so you want it recursive? Why didn't you say so from the beginning?
    Forget about opendir, readdir, while, and for, just use

    perldoc File::Find
    [color=blue]
    > after a particular time stamp.[/color]

    perldoc -f -M
    [color=blue]
    > - Then for each file with a particular string in its name,[/color]

    perldoc -f index
    [color=blue]
    > access the
    > file and look for string
    > ':20C::RELA//49879'[/color]

    perldoc -f open
    perldoc -f index
    [color=blue]
    > - Based upon that do throw an error or proceed for some other tasks.[/color]
    [color=blue]
    > I did look at CPAN, but it is overwhelming. Still looking though.[/color]

    Good idea, but including error checking this is probably 15-20 lines of code
    max. I doubt that you will find something this simple on CPAN.

    jue


    Comment

    • Roel van der Steen

      #3
      Re: Perl Script for Parsing

      > Good idea, but including error checking this is probably 15-20 lines of code[color=blue]
      > max. I doubt that you will find something this simple on CPAN.[/color]

      Everybody writes this all the time. Today's implementation (untested):

      #/usr/bin/perl
      use strict;
      use warnings;
      use File::Find;

      my $root_dir = '/etc';
      my $time_stamp = time - 24 * 60 * 60; # last 24 hours
      my $regex = qr':20C::RELA//49879'; # your regex
      my @candidates;

      find(
      sub {

      my $cur_file = $File::Find::na me;
      return unless -f $cur_file && (stat($cur_file ))[9] >= $time_stamp;

      # opportunity for improvement here: the error does not need to be fatal
      open my $FILE, $cur_file or die "Cannot open file $cur_file\n";

      READLINE: while (<$FILE>) {
      push(@candidate s, $cur_file), last READLINE if /$regex/;
      }

      },

      $root_dir
      );


      {
      local $, = "\n";
      print @candidates;
      }

      __END__

      Regards, Roel

      Comment

      Working...