perl required output

Hi,

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

the string is like this--

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]

Note: I get so many lines in between the above snippet. this is just a snippet.

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 --

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

Suggest to handle special characters in the username.

Thanks
NT

perl -ne '/(userName :.*)/; print $1,"\n";' logfile

I tried this option already and it did not work for me.

some part of my code is like this--

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 the deisred result in my case.

Thanks
NT

Use this expression to match [usernames]

$line =~ /(\[.*\])/;

And then do this to remove the brackets

$line2 =~ s/(\[|\])//;

Does this make a diffrence as i am doing both in one.

Thanks
NT

I suspect the problem is not what you think it is because your code works fine with your data, for me anyway:

while(<DATA>){
   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";
   }
}

__DATA__
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]

and the output is:

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'