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

breaking-apart and printing an input file

Thread Next
February 19, 2009 19:42
breaking-apart and printing an input file
Message ID:
I have a single input file with entries as follows:

USER1 [20090101] note
bla bla bla
bla bla bla

USER2 [20090104] note
bla bla bla
bla bla bla


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

So calling it as ' 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 $/;

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.


Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About