Extract the word from the file and print it


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract the word from the file and print it
# 8  
Old 01-22-2015
The regular expression has virtualServerid without spaces. Your input has virtualServer id with a space in between.
# 9  
Old 01-22-2015
Thanks Balaje,

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

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 =~ /virtualServer id=\"(.*?)\"/) {
         print "$1\n";
         $VS++;
       }
      }
close FILE

# 10  
Old 01-23-2015
Code:
if ($line =~ /virtualServer id=\"(.*?)\"/) {
     push(@VS, $1);
}

# 11  
Old 01-23-2015
Thanks Balaje for your kind help.

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

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

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

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

Moderator's Comments:
Mod Comment Please use CODE tags for sample input and output as well as for sample code segments.

Last edited by Don Cragun; 01-24-2015 at 01:45 AM.. Reason: Add CODE tags.
# 12  
Old 01-24-2015
Code:
if ( $line =~ / - (.*?) connected to/ && $1 !~ /wup10[12]/ ) {
    print $1;
}

# 13  
Old 01-25-2015
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
# 14  
Old 01-26-2015
Has anything like this been tried yet?
Code:
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" />

Code:
#!/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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to extract the word after a particular keyword throughout the file..

Hi Everyone, Need help in extracting the hostname from the below output. Expected output: DS-TESTB-GDS-1.TEST.ABC.COM DS-TESTB-GDS-2.TEST.ABC.COM .... ... /tmp $ cat -n /tmp/patchreport 1 /usr/bin/perl /admin/bin/patch/applyPatches.pl --apply_patches... (4 Replies)
Discussion started by: thiyagoo
4 Replies

2. Shell Programming and Scripting

Search for a specific word and print only the word from the input file

Hi, I have a sample file as shown below, I am looking for sed or any command which prints the complete word only from the input file. Ex: $ cat "sample.log" I am searching for a word which is present in this file We can do a pattern search using grep but I need to cut only the word which... (1 Reply)
Discussion started by: mohan_kumarcs
1 Replies

3. Shell Programming and Scripting

Perl script to extract a word from the file

Hi everyone, I'm a perl newbie and need your help to extract a word inside the list of files with same pattern. <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <ns2:mycode xmlns:ns2="http://www.abcd.com/pqrs/acfSchema-2007a.xsd"> <id>10</id> <name>PaymentServices</name> ... (7 Replies)
Discussion started by: jhamaks
7 Replies

4. UNIX for Dummies Questions & Answers

How to print the specific word in a file.

Hi , My input file is below like that :- $cat abc.txt Service name: test_taf Service is enabled Server pool: test_tac Cardinality: 2 Disconnect: false Service role: PRIMARY Management policy: AUTOMATIC DTP transaction: false AQ HA notifications: true Failover type: SESSION... (3 Replies)
Discussion started by: sp001
3 Replies

5. Shell Programming and Scripting

extract a word from text file name

Hi i want to extract the word present before .txt in the text file. For example, Sample_ab_a.txt ----------> i need 'a' Sample_abc_b.txt -----------> i need 'b' Can anyone help me in getting the word extracted (5 Replies)
Discussion started by: Sindhuap
5 Replies

6. Shell Programming and Scripting

extract whole thing in word, leaving behind last word. - perl

Hi, i've a string /u/user/DTE/T_LOGS/20110622_011532_TEST_11_HD_120/HD/TESi T_11_HD_120/hd-12 i need to get string, like /u/user/DTE/T_LOGS/20110622_011532_TEST_11_HD_120/HD the words from HD should get deleted, i need only a string till HD, i dont want to use any built in... (4 Replies)
Discussion started by: asak
4 Replies

7. Shell Programming and Scripting

How to extract just a word from a File in Shell?

Hello Friends, I have a txt file which has data like this TNS Ping Utility for Solaris: Version 10.2.0.3.0 - Production on 23-MAR-2010 15:38:42 Copyright (c) 1997, 2006, Oracle. All rights reserved. Used parameter files: Used TNSNAMES adapter to resolve the alias Attempting to... (7 Replies)
Discussion started by: njafri
7 Replies

8. Programming

How to extract a sentences of word from a text file.

Hi , i have a text file that contain a story How do i extract the out all the sentences that contain the word Mon. in C++ I only want to show those sentences that contain the word mon eg. Monkey on a tree. Rabbit jumping around the tree. I am very rich, I have lots of money. Today... (1 Reply)
Discussion started by: xiaojesus
1 Replies

9. Shell Programming and Scripting

Print out just a word from the file

Hi, Would like to find a more suitable solution for the following. I have a file eg test.log. In this file, i have to find the line that has "Final rating" which is the starting of the line. I need to print out only 5.75 instead of the whole line using "grep". May I know what suitable command... (8 Replies)
Discussion started by: Kinki
8 Replies

10. UNIX for Dummies Questions & Answers

extract last word on line to new file

Can someone please help me with how to extract the last word on a line to a new file? I have a list of names like: Ms. Nell D. Bullock Mrs. Sherrie M Avent LINDA ANNETTE RUSSELL Mr. Jerome R. Harris Pandora Tyndall I want the new file to look like this: Bullock Avent RUSSELL Harris... (10 Replies)
Discussion started by: michieka
10 Replies
Login or Register to Ask a Question