Extract the word from the file and print it

I have a file which I am reading and then I need to extract a particualr word and if it matches the line.

2015-01-22 07:30:17,814000 +0900 [29133:main]/INFO:[configuration.manager/Cont'd_0031] -            <ns2:virtualServerid="PH11PK" />

Means if the line contain Virtual server I need to extract the id .

Code I wrote

#!/usr/bin/perl

#@filename = 'file.txt';
$VS=@_;
$1;

open (FILE, "file.txt") or die;

while (my $line= <FILE>) {
#foreach  $line (@filename)
   {
      if ($line =~ /Info : \[([a-zA-Z]+)\] - [([a-zA-Z]+)\]\/) {
         print ("Login successful for ID $2\n");
         $VS++;
       }
      else{
       print ("ID $2 not connected,please check!!");
     }

But it is not working

if ($line =~ /virtualServerid=\"(.*?)\"/) {
  $id = $1;
}

Welcome to the forum, please use CODE TAGS as required by the forum rules!

Thank you

1 Like

Thanks balaje,

But still the code is going into else loop instead of if... Can u help!!

Works for me

[user@host]$ cat file
2015-01-22 07:30:17,814000 +0900 [29133:main]/INFO:[configuration.manager/Cont'd_0031] - <ns2:virtualServerid="PH11PK" />
2015-01-22 07:30:17,814000 +0900 [29133:main]/INFO:[configuration.manager/Cont'd_0031] - <ns2:virtualServerid="ABC123" />
[user@host]$ cat test.pl
#! /usr/bin/env perl

open FH, "< file";
while ($line = <FH>) {
#    chomp ($line);
    if ($line =~ /virtualServerid=\"(.*?)\"/) {
      print "$1\n";
    }
}
close FH;

[user@host]$ ./test.pl
PH11PK
ABC123
[user@host]$ 

Can you post your code?

HI Balaje,

I again ran but of no output was generated. I shortened the code.

#!/ms/dist/perl5/bin/perl5.8
#!/usr/bin/perl

#@filename = 'abc.log';
#$VS;


open FILE, "< abc.log";

while ( $line= <FILE>) {
   #chomp ($line);
#foreach  $line (@filename)

      if ($line =~ /virtualServerid=\"(.*?)\"/) {
         print "$1\n";
       }
      #else{
      # print ("ID $1 not connected,please check!!");
     #}
}
close FILE;

Extract of the File:

2015-01-22 07:30:17,814000 +0900 [29133:main]/INFO:[configuration.manager/Cont'd_0030] -             <ns2:logonTime hour="7" minute="45"/>
2015-01-22 07:30:17,814000 +0900 [29133:main]/INFO:[configuration.manager/Cont'd_0031] -             <ns2:virtualServer id="PH12MC" host="xxx" port="xxxx" localHost="xx" localPort="xx"/>
2015-01-22 07:30:17,814000 +0900 [29133:main]/INFO:[configuration.manager/Cont'd_0032] -             <ns2:virtualServer id="PH11MC" host="xxxx" port="xxxx" localHost="xxxx" localPort="xx"/>
--
--
--
2015-01-22 07:30:17,814000 +0900  [29133:main]/INFO:[configuration.manager/Cont'd_0048] -         </ns2:session>
2015-01-22 07:30:17,814000 +0900  [29133:main]/INFO:[configuration.manager/Cont'd_0049] -     </ns2:transactions>

To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags

```text
 and 
```

by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums

1 Like

The regular expression has virtualServerid without spaces. Your input has virtualServer id with a space in between.

Thanks Balaje,

It worked now.:slight_smile: If i want to enhance this script and add all the extracted id's into an array. I did like this. Is it fine?

#!/ms/dist/perl5/bin/perl5.8
#!/usr/bin/perl

#@filename = 'abc.log';
@VS;


open FILE, "< abc.log";

while ( $line= <FILE>) {
   #chomp ($line);
#foreach  $line (@filename)

      if ($line =~ /virtualServer id=\"(.*?)\"/) {
         print "$1\n";
         $VS++;
       }
      }
close FILE
if ($line =~ /virtualServer id=\"(.*?)\"/) {
     push(@VS, $1);
}

Thanks Balaje for your kind help.

Can you help me with the regex for following to extract PH11MD and PH11PP:

2015-01-23 07:45:00,009000 +0900 [4043:pool-9-thread-1]/INFO:[tokyose.vssocket-PH11MD] - PH11MD connected to /XXXX:XX
2015-01-23 07:45:00,011000 +0900  [4043:pool-9-thread-1]/INFO:[tokyose.vssocket-PH11PP] - PH11PP connected to /XXXX:XX

But I also have the following format which I want to discard:

2015-01-23 07:30:28,791000 +0900 [4043:pool-5-thread-1]/INFO:[tokyose.vssocket-wup102] - wup102 connected to localhost/127.0.0.1:5000

i know the id's I want to discard.

i used the following regex but did not work

if ($line =~ / \[([a-zA-Z]+)\](.*) connected to \  /\/ ([\d\.\:]+)\] : (.*)/) {
  if ($1!==wp102|wp101){
     print $1;
	 }
	 }
if ( $line =~ / - (.*?) connected to/ && $1 !~ /wup10[12]/ ) {
    print $1;
}

Thanks balaje for the reply.

the regex you provided is working for "connected to" and not for "connected to/" Can you please help have a look

Has anything like this been tried yet?

2015-01-22 07:30:17,814000 +0900 [29133:main]/INFO:[configuration.manager/Cont'd_0030] - <ns2:logonTime hour="7" minute="45"/>
2015-01-22 07:30:17,814000 +0900 [29133:main]/INFO:[configuration.manager/Cont'd_0031] - <ns2:virtualServer id="PH12MC" host="xxx" port="xxxx" localHost="xx" localPort="xx"/>
2015-01-22 07:30:17,814000 +0900 [29133:main]/INFO:[configuration.manager/Cont'd_0032] - <ns2:virtualServer id="PH11MC" host="xxxx" port="xxxx" localHost="xxxx" localPort="xx"/>
2015-01-22 07:30:17,814000 +0900 [29133:main]/INFO:[configuration.manager/Cont'd_0048] -         </ns2:session>
2015-01-22 07:30:17,814000 +0900 [29133:main]/INFO:[configuration.manager/Cont'd_0049] -     </ns2:transactions>
2015-01-23 07:45:00,009000 +0900 [4043:pool-9-thread-1]/INFO:[tokyose.vssocket-PH11MD] - PH11MD connected to /XXXX:XX
2015-01-23 07:45:00,011000 +0900 [4043:pool-9-thread-1]/INFO:[tokyose.vssocket-PH11PP] - PH11PP connected to /XXXX:XX
2015-01-23 07:30:28,791000 +0900 [4043:pool-5-thread-1]/INFO:[tokyose.vssocket-wup102] - wup102 connected to localhost/127.0.0.1:5000
2015-01-22 07:30:17,814000 +0900 [29133:main]/INFO:[configuration.manager/Cont'd_0031] - <ns2:virtualServerid="PH11PK" />
2015-01-22 07:30:17,814000 +0900 [29133:main]/INFO:[configuration.manager/Cont'd_0031] - <ns2:virtualServerid="ABC123" />
#!/usr/bin/env perl

use strict;
use warnings;

my $fil = './abc.log';
my $line;
my @VS;

open FPDATA, $fil or die "Can't open $fil";
while ($line = <FPDATA>) {
    chomp ( $line );
    if ( $line =~ "id=\"([[:alnum:]]*).*/" )
    {
        push(@VS, $1);
    }
    elsif ( $line =~ "socket-([[:alnum:]]*).*\]" && $line !~ "wup")
    {
        push(@VS, $1);
    }
}
close (FPDATA);

foreach my $id (@VS)
{
    print ("Login successful for ID $id\n");
}

# eof #

# output
# ------
# Login successful for ID PH12MC
# Login successful for ID PH11MC
# Login successful for ID PH11MD
# Login successful for ID PH11PP
# Login successful for ID PH11PK
# Login successful for ID ABC123

Hi

I did try:

#!/ms/dist/perl5/bin/perl5.8
#!/usr/bin/perl

#@filename = 'abc.log';
@VS;


open FILE, "< abc.log";

while ( $line= <FILE>) {
   #chomp ($line);
#foreach  $line (@filename)

#      if ($line =~ /virtualServer id=\"(.*?)\"/) {
       if ($line =~ /virtualServer id=\"(.*?)\"/){
        # print "$1 id is registered \n";
         push (@VS ,$1);
        }
      if ( $line =~ "socket-([[:alnum:]]*).*\]" && $line !~ "wup"){
       print $1;


}}
close FILE;

no output is printed for 2nd part.

Hmmm.. Does it make any difference if you change the quotes to slashes?
Maybe I should have kept quiet, because I don't know the differences between MS perl and linux perl?( if there are any ). One would think the code would be portable ( except for line endings ).

if ( $line =~ /socket-([[:alnum:]]*).*\]/ && $line !~ /wup/)
    {
        print ("$1\n");
    }

OK no problem.. thanks for trying.!!

Balaje,

If you can please throw some light.

Thanks in advance

Hi Balaje,

Can you please help here

Why you don't use awk ?

awk -F"[=\"]" '/virtualServer/ {print "id="$2,$3}OFS=""' <file

@karan8810: Not sure if you understood what push(@VS, $1) does.

In the first if-block, you're using push, where as in the second if-block you're printing $1 directly. And you're not printing the values populated in @VS in the first if-block. So that's why you're not getting the output as expected.

You could do something like this:

while ( $line= <FILE>) {
    chomp ($line);

    if ($line =~ /virtualServer id=\"(.*?)\"/) {
       push (@VS ,$1);
    }
    if ( $line =~ "socket-([[:alnum:]]*).*\]" && $line !~ "wup"){
       push (@VS, $1);
    }

}
close FILE;
print "@VS";

Also, did you try adapting ongoto's solution in post #14 ?