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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems in finding pattern....... 1

Status
Not open for further replies.

dphe

Programmer
Joined
Jan 15, 2007
Messages
16
Location
US
Hi All,

I have a pattern:

"GET /channel_contents?channel_id=$chanid&app_id=$appid.$appversion&stic_id=43D469EC837A4804AB57FF3056947D04&ticket=&authenticator=&stic_ver=2.13.1.14 HTTP/1.0" 200

where in $chanid, $appid, $appversion etc are dynamic variables.

Problem is that i'm not able to search for the pattern in a log file for the dynamic variables. Is there any way to do so , meant having dynamic variables in a pattern that needs to matched in a log file.

Since when I assign the same to another variable like:

$org = "\"GET \/channel_contents\?channel_id=$chanid\&app_id=$appid.$appversion\&stic_id=43D469EC837A4804AB57FF3056947D04\&ticket=\&authenticator=\&stic_ver=2.13.1.14 HTTP\/1.0\" 200";

and then search for the /$org/ pattern, it fails to do so. So is there any easier way to do this?

Thanks for the help in advance.
 
text from a file is treated as single-quoted strings, there is no variable interpolation. You expand variables in single-quoted strings using a regular expession and the 'e' option. An example:

Code:
my $lamb = 'wolf';
my $text = 'Mary had a little $lamb.';
$text =~ s/(\$\w+)/$1/eeg;
print $text;




- Kevin, perl coder unexceptional!
 
Your problem is with escaping.

First off, your defining of $org is a little misguided. It appears that you're trying to escape the regular expression special characters, but your only needlessly escaping them in your string assignment. Also, you use of "" as your string definition means that you have to escape double quotes within. It would be easier to use qq{} for your string assignment to avoid that need.

Finally, to escape special characters within a regular expression use quotemeta, or the \Q operator.


Your string assignment and regex will look like this:

Code:
my $org = qq{"GET /channel_contents?channel_id=$chanid&app_id=$appid.$appversion&stic_id=43D469EC837A4804AB57FF3056947D04&ticket=&authenticator=&stic_ver=2.13.1.14 HTTP/1.0" 200};

$testme =~ /\Q$org\E/;
 
oops, did I mis-understand the question? [flush]

- Kevin, perl coder unexceptional!
 
Cheers Kev. Just have another beer or a glass of sake as the case me be for some of us :)

[cheers]
 
Thnkx for the reply Kevin... The following is wht iam working on:

#Reading log file:

$infile='temp.log';
undef $/; #slurp mode
open IN, $infile or die $!;
$intext = <IN>;


$org = "\"GET \/channel_contents\?channel_id=$chanid\&app_id=$appid.$appversion\&stic_id=43D469EC837A4804AB57FF3056947D04\&ticket=\&authenticator=\&stic_ver=2.13.1.14 HTTP\/1.0\" 200";

if(($intext =~ m/$orgtext/)!= 0)
{open OUT, ">>$outfile" or die $!;
print OUT "\t$true --- Verified the Access logs successfully \n";}
else
{open OUT, ">>$outfile" or die $!;
print OUT "\t$fail --- Verfication of Access logs failed \n";}
close IN;

What can i do to enhance this or make it work?
 
Thnkx Miller for the reply.... but thn please do check in on the prev post... thnkx again..
 
dphe:

I've given you all the information that you need. It's up to you to put it together. Heck, you don't even have to put it together, you just gotta plug it in.

I'll let other community members chime in with the standard "use strict;"! and "use warnings;"! speeches, as your "$org" and "$orgtext" variable names look a little suspicious.
 
Thankx a lot Miller.. its working gr8.. no issues with the $org/$orgtext.. since I'd formed the post from the prev one and the other part from the script. Sorry to have confused. But thn its working gr8....
 
Guys one more question on patterns to be matched:

I have the following XML pattern:

<Update UpdateId="xyz" SpinId="xyz" StageId="xyz">

I need to check the number of occurrence of the pattern ignoring the value "xyz" in the above. The value "xyz" will change multiple number of times.

Need to check for the occurrence of the above tag... how will i do that?
 
You can either use a regex to match the pattern, or read the file in with XML::Simple and iterate over the resulting structure to process them. As XML already has a structure, why re-invent the wheel?

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]
 
Hey thnkx steve... just ran through with regex.. matching patterns... increased a counter and waoola its done...
 
Good. BTW, how do you pronounce waoola?

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