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
# 15  
Old 01-26-2015
Hi

I did try:

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=\"(.*?)\"/) {
       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.
# 16  
Old 01-26-2015
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 ).
Code:
if ( $line =~ /socket-([[:alnum:]]*).*\]/ && $line !~ /wup/)
    {
        print ("$1\n");
    }


Last edited by ongoto; 01-26-2015 at 03:09 AM..
# 17  
Old 01-26-2015
OK no problem.. thanks for trying.!!

Balaje,

If you can please throw some light.

Thanks in advance
# 18  
Old 01-27-2015
Hi Balaje,

Can you please help here
# 19  
Old 01-28-2015
Why you don't use awk ?

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

# 20  
Old 01-29-2015
Quote:
Originally Posted by karan8810
Code:
       if ($line =~ /virtualServer id=\"(.*?)\"/){
        # print "$1 id is registered \n";
         push (@VS ,$1);
        }
      if ( $line =~ "socket-([[:alnum:]]*).*\]" && $line !~ "wup"){
       print $1;
}}

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