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

Hi Monks

This is probably really simple but I cannot for the life of me find any material on this.

I need to write a script that allows someone to enter a reference code when prompted, I already have this but.... I need the script to search a directory full of other directories for a file containing that string which I assume I can use grep for, the difficult bit, for me, is I want to grep the file and then read it into an array like this
pseudo code: #!/usr/bin/perl -w use strict; use warn; my $REF; my @LINES; my $DPT_DIR=/shares/test/PPdir/ print "ENTER REF NUMBER"; chomp($REF = <>); grep "$REF" */*.dpt open my $FH, '<', *.dpt file grep has found chomp(@LINES, $FH); close $FH

Any help would be greatly appreciated.

Thanks

Jim

EDIT: Apologies it has be come apparent to me this isn't very clear, I don't need the list of files in an array but the contents of a single file.
Linux grep: #grep -l ABC123 */*.dpt lloydf/test.dpt <-- I need to read this file.
there are going to be several *.dpt all containing different refs which is why I need to do a grep/find

Replies are listed 'Best First'.
Re: Grep file out of directory and read into array
by pme (Monsignor) on Dec 08, 2014 at 13:59 UTC
    Hi Jim,

    See 'man File::Find' or you can use find command like this @files = `find . -name "pattern"`; if you are on linux.

    Regards

    UPDATE: This is a gentle introduction: Beginners guide to File::Find

      Hi pme

      So I have tried your suggestion and I am now getting this error

      Can't use string ("find . -name *.dpt | xargs grep "...) as a symbol ref while "strict refs" in use at ./perlref.pl line 14, <> line 1.

      Here is my code as it stands can you see anything the pops out at you

      #!/usr/bin/perl use strict; my $REG; my @REGARRAY; my $INI_DIR='/shares/MILKLINK/PPdir'; print "Enter Reg Number: "; chomp ($REG = <>); my $FILE = ('find %s *.dpt | xargs grep -l %s',$INI_DIR,$REG); open $FILE, '<', my $DPT; chomp(@REGARRAY, $FILE); close $FILE; foreach my $ITEM (@REGARRAY) { system("/shares/optiload/prog/indref.sh $ITEM"); }
      The file it should pick up is test.dpt
      PX12RUJ PX12RUR PX12RUV #PX12RUU PX12WLJ #PX12WLL PX12WLK PX12RUW WN14YGV WN14YGY
        Hi Jim,

        Try this:

        #!/usr/bin/perl use strict; use Data::Dumper; my $INI_DIR='/shares/MILKLINK/PPdir'; my $REG; print "Enter Reg Number: "; chomp ($REG = <>); my @FILE = `find $INI_DIR -name *.dpt | xargs grep -l $REG`; print Dumper(\@FILE) . "\n";
        Those quotes around 'find' are backquotes.

        It is unclear to me what you want to do with the grepped lines.

      Thankfully yes this is for a linux box, I'll take a look!

      thank you.

Re: Grep file out of directory and read into array
by marto (Cardinal) on Dec 08, 2014 at 14:03 UTC
Re: Grep file out of directory and read into array
by RichardK (Parson) on Dec 08, 2014 at 15:44 UTC

    I find File::Find::Rule much easier to use that File::Find and for this case even has a grep method.

    So you code to get the file-names containing your pattern(s) could look something like this (untested )

    my @files = File::Find::Rule->file() ->name( '*.dpt' ) ->grep( $pattern ) ->in( $dir );
      Same deal, less(?) typing :) also untested
      use File::Find::Rule qw/ find rule/; my @files = rule()->file() ->name( '*.dpt' ) ->grep( $pattern ) ->in( $dir );
      or even
      my @files = find( file => name => '*.dpt', grep => qr/pattern/, in => $dir, );
      or even
      my @files = rule( file => name => '*.dpt', grep => qr/pattern/, )->in( + $dir );
      or even an "iterator" version like foreach(@files) but without keeping a list of all the files in memory
      rule( file => name => '*.dpt', grep => qr/pattern/, )->exec(sub { my( $shortname, $path, $fullname ) = @_; ## do something with $fullname :) return !!0; # discard filename } ) ->in( $dir );