The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Read each word from File1 and search each file in file2 clem2610 Shell Programming and Scripting 8 04-23-2009 09:13 AM
search a word from file vishy Shell Programming and Scripting 9 02-08-2008 05:15 PM
search file for word, then assign to variable dejit UNIX for Dummies Questions & Answers 2 03-06-2007 01:57 PM
Finding word in file then print the preceding.... g_jumpin Shell Programming and Scripting 5 10-05-2006 11:31 AM
Print out just a word from the file Kinki Shell Programming and Scripting 8 02-20-2005 11:40 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-30-2009
becksram123 becksram123 is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 6
search a word in a xml file and print the out put

hi ,
i m having a html file and this file looks like this
Code:
 <ssl>
      <name>PIA</name>
      <enabled>true</enabled>
      <listen-port>39370</listen-port>
    </ssl>
    <log>
      <name>PIA</name>
    </log>
    <execute-queue>
      <name>weblogic.kernel.Default</name>
      <thread-count>50</thread-count>
    </execute-queue>
    <listen-port>37390</listen-port>
i need to search only listen -port and i need to take a value from here i.e 39370,37390

thanks in advance

with regards ,
ram

Last edited by Yogesh Sawant; 07-01-2009 at 04:32 AM.. Reason: added code tags
  #2 (permalink)  
Old 07-01-2009
anchal_khare anchal_khare is offline
Registered User
  
 

Join Date: Jun 2007
Location: Mumbai,India
Posts: 325
you can try something like this:

Code:
awk 'BEGIN { FS=" "; OFS=" " }
 
{
 
        if ( $0 ~ /<listen-port>/  &&  $0 ~ /<\/listen-port>/ ){
                # <listen-port>39370</listen-port>
                split($0,A,"\\076")
 
                # 39370</listen-port>
                split(A[2],LP,"\\074")
 
                listen_port=LP[1]
                printf("listen port is  %s\n",listen_port)
        }
 
}
 END {
 
 }' xmlfile
  #3 (permalink)  
Old 07-01-2009
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,088
even below may help you some. But if it is well-formatted XML, suggest you t o use XML parser in Perl to handler this kind of issue.


Code:
sed -n '/<listen-port>/{s/<listen-port>\(.*\)<\/listen-port>/\1/;p;}' yourfile
  #4 (permalink)  
Old 07-01-2009
prasperl prasperl is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 22
Try this in perl!

You can try this in perl!

Code:
#!/usr/local/bin/perl
open( INPUT_FILE, "$ARGV[0]" ) || die "Cannot open input file $ARGV[0]\n";
while ($line = <INPUT_FILE>)
        {
                chop $line;
                @base=split(/\>/,$line);
                $base[0] =~ s/[\<]*//;
                $LengthOfTag=length($base[0]);
                $TotalLength=length($line);
                $FinalLength=$TotalLength - $LengthOfTag - $LengthOfTag - 5;
                $val=substr($line,$LengthOfTag+2,$FinalLength);
                if ( $base[0] eq "listen-port" )
                {
                        print "$val\n";
                }
        }
close(INPUT_FILE);

Last edited by Yogesh Sawant; 07-01-2009 at 04:31 AM.. Reason: added code tags
  #5 (permalink)  
Old 07-01-2009
lathavim lathavim is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 73
grep '<listen-port>' filename | cut -d '>' -f2 | cut -d '<' -f1
  #6 (permalink)  
Old 07-01-2009
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,536
GNU awk
Code:
awk 'BEGIN{ RS="</listen-port>"}{gsub(/.*<listen-port>/,"")}1' file
  #7 (permalink)  
Old 07-01-2009
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,926
Code:
sed -n 's/<listen-port>\([[:digit:]][^<]*\).*$/\1/p'
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:28 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0