Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parse string question 1

Status
Not open for further replies.

proggybilly

Programmer
Apr 30, 2008
110
US
I have a script I am writing, keep in mind I am a major newbie, which pulls the lines out of a log file containing the word LOGIN.

Each line has an email address with in it.

I am using this code:
Code:
$zuser = substr $mail_line, index($mail_line,'user');

To pull the data out of the string starting at user till the end of the line. An example line would look like this:

Jun 18 10:44:06 server pop3d: LOGIN, user=myuserid@mydomain.com, ip=[::ffff:XXX.XXX.XXX.XXX]

What i need to do is pull the username part of the email address out of that line.

Can anyone please lend a hand with this?
 
What about:
Code:
my ( $username ) = $line =~ /user=([^@]+)@/;

 
Just a slight change to ishnid's code and it should work just fine:
Code:
my ( $username ) = [blue][b]([/b][/blue]$line =~ /user=([^@]+)@/[blue][b])[/b][/blue];
 
Thanks both of you, it is working now!!! This is my first attempt at PERL, way it goes, boss tosses me a project and I have to learn as I go.
 
It shouldn't need those extra parenthesis. The =~ operator binds higher than assignment. The following self-contained script gives me the correct output:
Code:
#!/usr/bin/perl -w
use strict;

my $line = <DATA>;
my ( $username ) = $line =~ /user=([^@]+)@/;
print "$username\n";

__DATA__
Jun 18 10:44:06 server pop3d: LOGIN, user=myuserid@mydomain.com, ip=[::ffff:XXX.XXX.XXX.XXX]
 
Guess I should have tested it before opening my mouth. You're right ishnid.
 
proggybilly said:
That just ends up giving me a 1
did you miss off the parentheses on the lvalue? Without those it will evaluate in a scalar context, which would give you the '1'.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top