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
# 1  
Old 01-21-2015
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.
Code:
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
Code:
#!/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


Moderator's Comments:
Mod Comment
Please wrap all code, files, input & output/errors in CODE tags
It makes them far easier to read and preserves multiple spaces

Last edited by rbatte1; 01-22-2015 at 08:38 AM.. Reason: Added CODE tags
# 2  
Old 01-22-2015
Code:
if ($line =~ /virtualServerid=\"(.*?)\"/) {
  $id = $1;
}

# 3  
Old 01-22-2015
Welcome to the forum, please use CODE TAGS as required by the forum rules!

Thank you

Last edited by fpmurphy; 01-22-2015 at 04:22 AM..
This User Gave Thanks to sea For This Post:
# 4  
Old 01-22-2015
Thanks balaje,

But still the code is going into else loop instead of if... Can u help!!
# 5  
Old 01-22-2015
Works for me

Code:
[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?
# 6  
Old 01-22-2015
HI Balaje,

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

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:

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>


Last edited by rbatte1; 01-22-2015 at 08:39 AM.. Reason: Added CODE tags
# 7  
Old 01-22-2015
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 [code] and [/code] 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
This User Gave Thanks to fpmurphy For This Post:
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