minister has asked for the wisdom of the Perl Monks concerning the following question:

Edited by Corion: Moved to Seekers of Perl Wisdom

Hail Perl Monks:

I'm a Perl novice and could use some help with a regular expression job -- I strip out some data in a flat file and then convert it XML. The file looks like this:
...
URL=<http://www.1033edge.com/>
address=efmail@1033edge.com
username=efmail
password=testing
URL=<http://www.1033edge.com/>
address=efmail12@1033edge.com
username=efmail12
password=testing
URL=<http://www.1033edge.com/>
address=efmail2@1033edge.com
username=efmail2
password=testing
...

Ad naseum. If someone could help me out with a regular expression that would strip out everything but the email addresses (the stuff to the right of "address") and pipe it another text file, I will worship you forever. Forgive a novice programmer.

- Patrick (pnichols@uucom.com)

Replies are listed 'Best First'.
RE: Perl Fool needs help
by tye (Sage) on Aug 14, 2000 at 19:58 UTC
    perl -lne 'print $1 if m/^address=(.*)/'

    This probably makes more sense as a posting to the Seekers of Perl Wisdom section.

            - tye (but my friends call me "Tye")
RE: Perl Fool needs help
by turnstep (Parson) on Aug 14, 2000 at 20:07 UTC

    It's a quick and easy command line solution, although it (like all command-line scripts) could get a little bit ugly because of having to escape some characters for the shell (this is for unix):

    perl -l -n -e "/address=(.*)/ and print \$1;" yourfile.txt > emails.tx +t

    If your example is really as simple as it appears, a simple expression like address=(.*) should do the trick. You could be more paranoid and make it

    /\s*address=(.*)/
    which would only match lines that started with "address", not counting spaces, tabs, etc. You could also play around with using the @ sign. It all depends on exactly what your data looks like as to how complicated the regular expression could possibly get, but the above will probably catch most things.

RE: Perl Fool needs help
by qi3ber (Scribe) on Aug 14, 2000 at 21:52 UTC
    If I am reading your question correctly, this is most likely closer to what you are looking for.

    perl -ne 'print if not m/address/; print $1 if m/^(address=)/;'


    That'll give you everything except for the email address which is found after the equal sign on the address line.


       [ qi3ber ] I used to think that I knew it all, then I started to listen.
RE: Perl Fool needs help
by wardk (Deacon) on Aug 15, 2000 at 02:01 UTC
    #!/usr/local/bin/perl open INFILE, "<myInfile.txt" or die "cannot open myInfile.txt $!\n"; open OUTFILE, ">myOutfile.txt" or die "cannot open myOutfile.txt $!\n" +; while ( <INFILE> ) { if ( /^address/ ) { chomp; my ($tag, $value) = split(/=/, $_); print OUTFILE $value, "\n"; } } close INFILE; close OUTFILE;
RE: Perl Fool needs help
by Anonymous Monk on Aug 16, 2000 at 00:57 UTC
    One way to do it. open(FILE, $infile) || die "Can't open $infile for read"; @data = <FILE>; close(FILE); open(FILE, ">$outfile") || die "Can't open $outfile for write"; foreach (@data) { if($_ =~ /\@/) { ($junk, $email) = split(/=/, $_); print FILE $email; } } close(FILE);
      I'll try that again .. @data = <FILE>;
RE: Perl Fool needs help
by ybiC (Prior) on Aug 15, 2000 at 15:19 UTC
    So does this make August 14th "A-Perl Fools Day"</t> ?   <big grin>

    Where's a personal drummer when you need one?
        ybiC