Hi PerlMonks!

First please allow me to apologize for my rookie question for which I seek enlightenment :)

I'm very new when it comes to programming, specially perl (which I'm enjoying very much so far)

I've got an apparent simple problem that I can't quite figure out. Here's the scenario:

- On the command line I can pass several parameters, each containing strings or files paths separated by pipes: script.pl --id1="test1|test2" --id2="test3|test4" --file="file1.txt|file2.txt"

- The files can be in several directories, so placing them all in one directory and simply reading it wouldn't be an option, I guess

- I wish to use the strings on parameters "id1" and "id2" to search all the specified files on parameter "file" and then print out the file name with the line numbers that match (or even better, output this to a brand new file)

So far, I've managed to write the following code after doing some readings, which works well if I specify only one file:

#!usr/bin/perl use strict; use warnings; use Getopt::Long; GetOptions( 'id1=s' => \my $id1, 'id2=s' => \my $id2, 'file=s' => \my $file, ); open (my $filename, "<", $file) or die "Could not open $file, $!"; while (<$filename>){ while (/$id1/g && /$id2/g) { print "$file:$.\n"; } } close ($filename);

If I run as is, with only one file specified, it works fine, except for the print part, where I'm getting globs instead of the actual filenames:

GLOB(0xa5a36c):61

GLOB(0xa5a36c):65

So moving on to the actual questions:

1. How could I loop through all the files and open them line by line and search for the matching strings?

2. How could I output the matches to a new file?

3. How could I list the actual matching file names instead of the globs?

Thank you all very much and I'm sorry if I wasn't clear enough on my intentions, for which I apologize. Any guidance or directions towards the right path are highly appreciated

With best regards,

Leiria


In reply to Open several files and read line by line by Leiria

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.