Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-03-29 07:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found