In Perl you would do something like this:

perl -lne 'print if /pattern/' filename

If you were not constrained to use Perl you would probably do this instead:

grep 'pattern' filename

Anywhere that comes with Perl pre-installed, you probably already have grep too. Anywhere that Perl does not come pre-installed, installing Perl takes longer than installing a few GNU tools (even on Windows there's that whole Ubuntu on Windows thing, plus many older solutions that provide essential shell tools).

Perl is useful for this kind of thing because it can be extended far beyond the simple semantics of GNU grep. But if your needs actually are simple, the simpler tool is grep. If this is a learning exercise, then definitely go down the Perl route. If it's just to get something done, consider grep, where you can read a single manpage and learn it all, versus a full featured programming language where perlintro is just the start.

So if you do go with Perl for this, it might come in handy to learn what it is that one-liner up above is doing, and how it works. If you were to convert that one-liner to a proper script, it should look more like this:

use strict; use warnings; while (defined(my $line = <>)) { chomp $line; if ($line =~ m/pattern/) { print "$line\n"; } }

The <> operator reads from the file presented on the commandline (found in the @ARGV array), one line at a time. If there is a line to read, the operation will have a value that makes the defined() condition true. So after there are no more lines to read, the condition becomes false and you stop looping. chomp removes the trailing newline from each line. Then you test if the line matches a pattern. If it does, you print that line out with a trailing newline.


Dave


In reply to Re: Simple search and output by davido
in thread Simple search and output by rvaughans

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.