develooper Front page | perl.beginners | Postings from February 2009

breaking-apart and printing an input file

Thread Next
From:
pablo
Date:
February 19, 2009 19:42
Subject:
breaking-apart and printing an input file
Message ID:
20090220034232.GA6385@arch
I have a single input file with entries as follows:

--snip--
USER1 [20090101] note
bla bla bla
bla bla bla

USER2 [20090104] note
bla bla bla
bla bla bla

--snip--

What I'm trying to do is create a single-argument script which displays
all entries for a given user.

So calling it as 'filter.pl user1' will print all entries for USER1.


Here is what I've got so far:

---
my $user = shift;

# slurp file
my $file = do {
    open my $fh, 'inputfile.txt' or die $!;
    local $/;
    <$fh>;
};

if ( $file =~ /(^$user.*?)^$/smg ) {
        print $1;
}
---

Here's my thinking behind the non-functional regex.
It matches a set of lines starting with a line which begins with $user,
followed by all non-empty lines, and terminated with an empty line (^$).

Running my script produces zero output.  My questions are:
1. Is there a simpler way to approach/accomplish this?
2. If not, what's wrong with my regex?



Thanks in advance for any help.

/P

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About