CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
Output mismatch

 



perlisgreat
Novice

Aug 13, 2009, 2:18 AM

Post #1 of 10 (4926 views)
Output mismatch Can't Post

Hi,

I have a string in log file from that i need to pick the username.

the string is like this--


Code
pid 2172 tid 3124: 160: 10110847: userName :[app@abc.com] 
pid 2172 tid 3124: 160: 10110847: userName :[napp@abc.com]
pid 2172 tid 3124: 160: 10110847: userName :[12345]
pid 2172 tid 3124: 160: 10110847: userName :[app@abc_sde.com]
pid 2172 tid 3124: 160: 10110847: userName :[567890]
pid 2172 tid 3124: 160: 10110847: userName :[dfgehj]


I want to take all the usernames here in the snippet. The code i have gives only the numeric part.

The output i am expecting is --


Code
app@abc.com 
napp@abc.com
12345
app@abc_sde.com
567890
dfgehj


Suggest to handle special characters in the username.

Thanks
NT


(This post was edited by perlisgreat on Aug 13, 2009, 4:37 AM)


ichi
User

Aug 13, 2009, 3:00 AM

Post #2 of 10 (4925 views)
Re: [perlisgreat] Output mismatch [In reply to] Can't Post

so where is your code?


perlisgreat
Novice

Aug 13, 2009, 3:07 AM

Post #3 of 10 (4923 views)
Re: [ichi] Output mismatch [In reply to] Can't Post

Here is my piece of code---

I am just pasting some part.


Code
for(;$index<@records;$index++) 
{
#printf "LINENO=$index\n";

if ($records[$index] =~ /userName\s+:\[(..*)\]/)
{
$User_Name = $1;
print "User_Name before concatenation is = $User_Name\n";
$User_Name =~ s/\s+//g;
$User_Name= "'" . $User_Name . "'";
print "User_Name = $User_Name\n";
}


I never got a username with special characters in my case.

Thanks
NT


ichi
User

Aug 13, 2009, 3:29 AM

Post #4 of 10 (4920 views)
Re: [perlisgreat] Output mismatch [In reply to] Can't Post


Code
while (<>){ 
chomp;
@s = split /:/;
print join(":",$s[-2],$s[-1]) . "\n";
}

output

Code
# more file 
pid 2172 tid 3124: 160: 10110847: userName :[app@abc.com]
pid 2172 tid 3124: 160: 10110847: userName :[napp@abc.com]
pid 2172 tid 3124: 160: 10110847: userName :[12345]
pid 2172 tid 3124: 160: 10110847: userName :[app@abc_sde.com]
pid 2172 tid 3124: 160: 10110847: userName :[567890]
pid 2172 tid 3124: 160: 10110847: userName :[dfgehj]

# perl perl.pl file
userName :[app@abc.com]
userName :[napp@abc.com]
userName :[12345]
userName :[app@abc_sde.com]
userName :[567890]
userName :[dfgehj]



perlisgreat
Novice

Aug 13, 2009, 4:59 AM

Post #5 of 10 (4916 views)
Re: [ichi] Output mismatch [In reply to] Can't Post

hi,

There are many lines in the log file which can be split with a colon but i do not want that in my output. Sorry for the misguidance.

Thanks
NT


ichi
User

Aug 13, 2009, 8:16 AM

Post #6 of 10 (4913 views)
Re: [perlisgreat] Output mismatch [In reply to] Can't Post


In Reply To
hi,

There are many lines in the log file which can be split with a colon but i do not want that in my output. Sorry for the misguidance.

Thanks
NT

come on, give it a try. you can split on colons, get the last field , which is what you want (right?) . then do substitution of [ and ] . I am sure you know how to do it by now. If not , review your regex documentation AND your Perl fundamentals.


(This post was edited by ichi on Aug 13, 2009, 8:17 AM)


spider
User


Aug 14, 2009, 2:59 AM

Post #7 of 10 (4896 views)
Re: [perlisgreat] Output mismatch [In reply to] Can't Post

The problem must be outside the code you are showing.
Your regexp looks ok. (..*) should catch everything, and should be the same as (.+)
I tested your code with only small adjustments to be able to test this part.
in_test:

Code
pid 2172 tid 3124: 160: 10110847: userName :[app@abc.com] 
pid 2172 tid 3124: 160: 10110847: userName :[napp@abc.com]
pid 2172 tid 3124: 160: 10110847: userName :[12345]
pid 2172 tid 3124: 160: 10110847: userName :[app@abc_sde.com]
pid 2172 tid 3124: 160: 10110847: userName :[567890]
pid 2172 tid 3124: 160: 10110847: userName :[dfgehj]


The code with only small adjustments:

Code
#!/usr/bin/perl 
use warnings ;
open(FILE,"in_test") ;
while(<FILE>){
if ($_ =~ /userName\s+:\[(..*)\]/)
{
$User_Name = $1;
print "User_Name before concatenation is = $User_Name\n";
$User_Name =~ s/\s+//g;
$User_Name= "'" . $User_Name . "'";
print "User_Name = $User_Name\n";
}
}
close(FILE) ;



The output:


Code
User_Name before concatenation is = app@abc.com 
User_Name = 'app@abc.com'
User_Name before concatenation is = napp@abc.com
User_Name = 'napp@abc.com'
User_Name before concatenation is = 12345
User_Name = '12345'
User_Name before concatenation is = app@abc_sde.com
User_Name = 'app@abc_sde.com'
User_Name before concatenation is = 567890
User_Name = '567890'
User_Name before concatenation is = dfgehj
User_Name = 'dfgehj'



spider
User


Aug 14, 2009, 3:12 AM

Post #8 of 10 (4894 views)
Re: [perlisgreat] Output mismatch [In reply to] Can't Post

By the way, I expect you know that your code is incomplete.
In the part of the code you have shown, the if statment is not closed.

It seem like you are going throug every element of an arry called @records. Then I would have prefered to use foreach in sted of for, but this is just my preference.


Code
foreach(@records)  
{
if ($_ =~ /userName\s+:\[(..*)\]/)
{
$User_Name = $1;
print "User_Name before concatenation is = $User_Name\n";
$User_Name =~ s/\s+//g;
$User_Name= "'" . $User_Name . "'";
print "User_Name = $User_Name\n";
}
}



perlisgreat
Novice

Aug 14, 2009, 3:35 AM

Post #9 of 10 (4889 views)
Re: [spider] Output mismatch [In reply to] Can't Post

Hi Spider,

Thnaks for the reply, but for clarification i have full code, but i pasted only that part that is suspicous to me.
I dont see any changes in the script, as still i do not get the desired result. I am not getting any string which has special characters in it.

I dont know why it i shappening with me.

Thanks
NT


spider
User


Aug 14, 2009, 4:44 AM

Post #10 of 10 (4885 views)
Re: [perlisgreat] Output mismatch [In reply to] Can't Post

Have you tried to seperate only this code (like I did), and tested the result?

When you don't know where an error happens, It is a good ide to add some more debug printoute.
ex. at the top of the for-loop. Add a print of the record, and see if all records are as you expected.
If they are, remove it, and do the same inside your if-statement, to see if that capture the records you expect.

Also, remeber to always add the following lines to the top of your code:
use strict ;
use warnings ;
And when you have trobles, also add
use diagnostics ;

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives