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!

Pattern matching is being greedy

Status
Not open for further replies.

rbalbatov

Technical User
Feb 5, 2008
1
US
I wrote a Perl scirpt that doesn't quite do what I want. The script is designed to go through a log file and pull out and list all the email addresses that exist between the strings 'Enter AccountManager.createAccount' and 'Exit. Return a status code: ' followed by any single digit. If


If anything other than a digit is encountered (after 'Exit. Return a status code:
), the script should just skip that entire segment and move onto the next segment. The problem is my script matches everything between the 1st 'Enter AccountManager.createAccount' and 'Exit. Return a status code: ‘.


Source of perl script:
use strict;
use warnings;
use diagnostics;

# Declare and Assign global variables
my $log_file;
my $line;
my @value;
my $value;

print "Please enter pim log location. \n";
$log_file = <STDIN>;
chomp($log_file);

open(LOG, "< $log_file");

$line = do { local $/; <LOG> }; # Read the entire contents of the source document and assign it to the scalar 'file'.
while ($line =~ /Enter AccountManager.createAccount(.*?)Exit. Return a status code: [1-9]/gs)
{
print "-----------------------------------------------------------\n";
@value = split(/ /, $1);
foreach $value (@value)
{
if ($value =~ m/@/)
{
print "$value\n";
}
}
}
close LOG;

===================================================================



Output from execution:

C:\logs\IMS>u11.pl
Please enter pim log location.
test.log
-----------------------------------------------------------
hobo1@acme.com

-----------------------------------------------------------
hobo2dontshow@acme.com

hobo3@acme.com

-----------------------------------------------------------
hobo4@acme.com

===================================================================
Note: In the output above, the email ‘hobo2dontshow@acme.com’ should not be displayed, but it is. Anybody have any idea where my logic is wrong?


===================================================================

Input log file:
02:42:005 ad89ec AccountCreationUtil: validateISPNewEmailAccount() - Exit. Return a status code: 2
I 12:03:04:526 1d44ae9 AccountManager: Enter AccountManager.createAccount()
I 12:03:04:570 1d44ae9 AccountCreationUtil: createAccount() - Enter with AccountInfo :
emailAddress hobo1@acme.com



The retrieved MobileUserId from the created IMS account is: 178
I 12:03:13:549 1d44ae9 AccountCreationUtil: createAccount() - InstallMessageSender.sendInstallSMSUsingMTS() was successful
I 12:03:13:570 1d44ae9 AccountCreationUtil: createAccount() - Exit. Return a status code: 9
1d44ae9 AccountManager: Enter AccountManager.createAccount()
I 12:03:04:570 1d44ae9 AccountCreationUtil: createAccount() - Enter with AccountInfo :
emailAddress hobo2dontshow@acme.com



The retrieved MobileUserId from the created IMS account is: 178
I 12:03:13:549 1d44ae9 AccountCreationUtil: createAccount() - InstallMessageSender.sendInstallSMSUsingMTS() was successful
I 12:03:13:570 1d44ae9 AccountCreationUtil: createAccount() - Exit. Return a status code: a

1d44ae9 AccountManager: Enter AccountManager.createAccount()
I 12:03:04:570 1d44ae9 AccountCreationUtil: createAccount() - Enter with AccountInfo :
emailAddress hobo3@acme.com



The retrieved MobileUserId from the created IMS account is: 178
I 12:03:13:549 1d44ae9 AccountCreationUtil: createAccount() - InstallMessageSender.sendInstallSMSUsingMTS() was successful
I 12:03:13:570 1d44ae9 AccountCreationUtil: createAccount() - Exit. Return a status code: 7

1d44ae9 AccountManager: Enter AccountManager.createAccount()
I 12:03:04:570 1d44ae9 AccountCreationUtil: createAccount() - Enter with AccountInfo :
emailAddress hobo4@acme.com



The retrieved MobileUserId from the created IMS account is: 178
I 12:03:13:549 1d44ae9 AccountCreationUtil: createAccount() - InstallMessageSender.sendInstallSMSUsingMTS() was successful
I 12:03:13:570 1d44ae9 AccountCreationUtil: createAccount() - Exit. Return a status code: 7

1d44ae9 AccountManager: Enter AccountManager.createAccount()
I 12:03:04:570 1d44ae9 AccountCreationUtil: createAccount() - Enter with AccountInfo :
emailAddress hobo5dontshow@acme.com



The retrieved MobileUserId from the created IMS account is: 178
I 12:03:13:549 1d44ae9 AccountCreationUtil: createAccount() - InstallMessageSender.sendInstallSMSUsingMTS() was successful
I 12:03:13:570 1d44ae9 AccountCreationUtil: createAccount() - Exit. Return a status code: a100
 
Try
I am not sure why your code is not working but have a try with following:

Code:
while ($line =~ /Enter AccountManager.createAccount(.*?)Exit. Return a status code:\s\d+/gs)

--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
Your regexp doesn't match where there is no figure, so it goes on to the next occurrence. Also I would make it more robust: as an example, included below, accept 0 or more spaces after 'status code:', but it is up to you, depending on the robustness of the source of the file, to define the possible variations.
With the regexp below you will accept any word character (letters+figures+underscore) as the status code, then you need to check if it's numeric.
Code:
while ($line =~ /Enter AccountManager.createAccount(.*?)Exit. Return a status code:\s*?(\w)/gs){
  my$found=$1;
  if($2=~/[1-9]/){
    print...etc
  }
}

Franco
: Online tools for structural design
: Magnetic brakes for fun rides
: Air bearing pads
 
Don't try to accomplish everything in a single regex. Instead break up your parsing to simplify your logic.

The below script does what you want. The only assumption it makes is that the return code occurs before the email address, and that there is always a return code. If this isn't the case, than you'll have to fix the logic. But this works for the data that you provided.

Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]
[black][b]use[/b][/black] [green]warnings[/green][red];[/red]
[black][b]use[/b][/black] [green]diagnostics[/green][red];[/red]

[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple]Please enter pim log location. [purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$log_file[/blue] = <STDIN>[red];[/red]
[url=http://perldoc.perl.org/functions/chomp.html][black][b]chomp[/b][/black][/url][red]([/red][blue]$log_file[/blue][red])[/red][red];[/red]


[url=http://perldoc.perl.org/functions/open.html][black][b]open[/b][/black][/url][red]([/red][black][b]my[/b][/black] [blue]$fh[/blue], [red]'[/red][purple]<[/purple][red]'[/red], [blue]$log_file[/blue][red])[/red] or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]Can't open [blue]$log_file[/blue]: [blue]$![/blue][/purple][red]"[/red][red];[/red]

[black][b]my[/b][/black] [blue]$code[/blue] = [red]'[/red][purple][/purple][red]'[/red][red];[/red]

[olive][b]while[/b][/olive] [red]([/red]<[blue]$fh[/blue]>[red])[/red] [red]{[/red]
	[black][b]chomp[/b][/black][red];[/red]
	
	[olive][b]if[/b][/olive] [red]([/red][red]/[/red][purple]Return a status code:[purple][b]\s[/b][/purple]+([purple][b]\S[/b][/purple]+)[/purple][red]/[/red][red])[/red] [red]{[/red]
		[blue]$code[/blue] = [blue]$1[/blue][red];[/red]
		
	[red]}[/red] [olive][b]elsif[/b][/olive] [red]([/red][red]/[/red][purple]^[purple][b]\s[/b][/purple]*emailAddress[purple][b]\s[/b][/purple]+([purple][b]\S[/b][/purple]+)[/purple][red]/[/red][red])[/red] [red]{[/red]
		[black][b]my[/b][/black] [blue]$email[/blue] = [blue]$1[/blue][red];[/red]
		
		[olive][b]if[/b][/olive] [red]([/red][blue]$code[/blue] =~ [red]/[/red][purple]^[purple][b]\d[/b][/purple]+$[/purple][red]/[/red][red])[/red] [red]{[/red]
			[black][b]print[/b][/black] [red]"[/red][purple][blue]$email[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
		[red]}[/red]
	[red]}[/red]
[red]}[/red]
close [blue]$fh[/blue][red];[/red]

[fuchsia]1[/fuchsia][red];[/red]

[teal]__END__[/teal]
[tt]------------------------------------------------------------
Pragmas (perl 5.10.0) used :
[ul]
[li]diagnostics - Produce verbose warning diagnostics[/li]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[li]warnings - Perl pragma to control optional warnings[/li]
[/ul]
[/tt]- Miller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top