in reply to Re: grabbing certain lines
in thread grabbing certain lines

This method assumes you're pulling the entire file into @data.

If you don't want to do that, filter the lines first:
my @lines = grep { /^(From|To):/ } <FILE>;

Replies are listed 'Best First'.
Re: Re: Re: grabbing certain lines
by imlou (Sexton) on Nov 11, 2002 at 22:20 UTC
    Well this is what I did
    if(/^(((From:)|(To:)|(Reply-To:)).+)/){ print "$1\n";
    This give me the whole line. i.e. From: misterbob <bob@mister.com> But now I want to get rid of the From:, To: or what ever is before the ":" and keep misterbob and <bob@moster.com>. I tried splitting on ":" but that didn't seem to work for me. Please advise. Thank you.
      I believe if you just change your code to:

      print $2 if ($text =~ /^(From|To|Reply-To):\s+(.+)/);

      That works for me in your test example.

      What you should be doing is looking for the .+ part of your regex and you're not saving that bit. You need to say (.+) and look at $2 and you should be fine. I also moved the : outside your or statement as there's no need that I know of to type that : 3 times when you can get away with once. :)

      Hope that helps!

      There is no emoticon for what I'm feeling now.

Re^3: grabbing certain lines
by adrianh (Chancellor) on Nov 12, 2002 at 09:45 UTC

    I belive you'll find that still reads the entire file in before running the grep - so filtering the file first doesn't really give you anything.

    If the size of the file is an issue you'll want to use a loop to go over it line by line. Something like this should do it:

    my %name2email; while (<DATA>) { next unless m/^(?:To|From|Reply-To): (\S+)\s+<?([^\s>]+)/; $name2email{$1} = $2; };

    Note: the regex doesn't cope with a fair bit (multiple addresses, addresses without names, etc.).