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
How to get lines started with matched strings using sed or grep for loop? AMBER Shell Programming and Scripting 8 07-13-2009 05:26 AM
Grep and delete lines except the lines with strings vj8436 Shell Programming and Scripting 14 04-17-2009 11:25 AM
To grep 10 lines after a string in a txt file. suman82 Shell Programming and Scripting 6 12-13-2008 01:08 AM
grep string & a few lines after ashterix Shell Programming and Scripting 7 12-07-2008 09:20 AM
grep string & next n lines ashterix Shell Programming and Scripting 8 11-21-2005 11:38 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 07-10-2009
bhagirathi bhagirathi is offline
Registered User
  
 

Join Date: Jul 2009
Posts: 10
grep a string in the lines between 2 strings of a file

Hi ,
Please help me with the following problem:
I have an xml file with the following lines
Code:
<cisco:name>
  <cisco:mdNm>Cisco Device 7500 A Series</cisco:mdNm>
  <cisco:meNm>10.1.100.19</cisco:meNm>
  <cisco:ehNm>/shelf=1</cisco:ehNm>
      <cisco:subname>
         <cisco:sptp>Cisco PortA Series</cisco:sptp>
         <cisco:aliasNameList xsi:nil="true"/>
         <cisco:owner xsi:nil="true"/>
                      <cisco:subportname>
                 <cisco:cpt>Cisco SubPort B Series</cisco:cpt>
                 <cisco:aliasNamesubList xsi:nil="true"/>
                 <cisco:userLabel xsi:nil="true"/>
               </cisco:subportname>
       </cisco:subname>
           <cisco:subname>
         <cisco:sptp>Cisco PortAB Series</cisco:sptp>
         <cisco:aliasNameList xsi:nil="true"/>
         <cisco:owner xsi:nil="true"/>
                      <cisco:subportname>
                 <cisco:cpt>Cisco SubPort AB Series</cisco:cpt>
                 <cisco:aliasNamesubList xsi:nil="true"/>
                 <cisco:userLabel xsi:nil="true"/>
               </cisco:subportname>
       </cisco:subname>
</cisco:name>
<cisco:name>
  <cisco:mdNm>Cisco Device 7500B Series</cisco:mdNm>
  <cisco:meNm>10.1.100.20</cisco:meNm>
  <cisco:ehNm>/shelf=2</cisco:ehNm>
      <cisco:subname>
         <cisco:sptp>Cisco Port B Series</cisco:sptp>
         <cisco:aliasNameList xsi:nil="true"/>
         <cisco:owner xsi:nil="true"/>
                      <cisco:subportname>
                 <cisco:cpt>Cisco SubPort B Series</cisco:cpt>
                 <cisco:aliasNamesubList xsi:nil="true"/>
                 <cisco:userLabel xsi:nil="true"/>
               </cisco:subportname>
       </cisco:subname>
</cisco:name>
In My code it will first check the two strings <cisco:name> and </cisco:name> and after that it will grep the count of <cisco:cpt>. and so on continue till the end of file


Code:
#!/bin/sh
sed '/<\/cisco:name>/{G;}' test.xml >temp 

nawk 'BEGIN{RS=""} 
{ 
grep -c '<cisco:cpt>' print 


}' temp 
rm temp
this code is not giving the the op as
in first<cisco:name> and </cisco:name> loop it should give 2 then for next it would 1

plz help
  #2 (permalink)  
Old 07-10-2009
honglus honglus is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 44
Code:
awk '{ if (  $0 ~ /<cisco:cpt>/  )  {i++} if ( $0 ~ /<\/cisco:name>/ ) {print i;i=0}}' test.xml
  #3 (permalink)  
Old 07-10-2009
bhagirathi bhagirathi is offline
Registered User
  
 

Join Date: Jul 2009
Posts: 10
Hi Honglus,
thanks it works fine. but i have one query
if i have to search two or 3 patterns like <cisco:cpt> , <cisco:mdNm> ,<cisco:sptp> then how can i proceed.
  #4 (permalink)  
Old 07-10-2009
honglus honglus is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 44
Quote:
Originally Posted by bhagirathi View Post
Hi Honglus,
thanks it works fine. but i have one query
if i have to search two or 3 patterns like <cisco:cpt> , <cisco:mdNm> ,<cisco:sptp> then how can i proceed.
link them with OR, something like this:
$0 ~ /(one|two|three)/
  #5 (permalink)  
Old 07-10-2009
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,088
more formally, perl can handle XML document with many availiable module.

You may try below to see the result
Code:
use XML::Parser;
my $file = 'a.xml';
my ($flag,$index,@arr,%hash)=(0,-1);
sub start_handler
{
    my $expat = shift; 
    my $element = shift;
    if($element eq 'cisco:name'){
			$index++;
		}
		else{
			if($element eq 'cisco:cpt'){
				$flag=1
			}
			else{
				$flag=0;				
			}
		}
}
sub char_handler
{
    my ($p, $data) = @_;
    if( ($flag eq '1') and  !($data =~ /^\s*$/)){
    	$arr[$index]++;
    	$hash{$index}.="  ".$data;
    }
}
my $parser = new XML::Parser(ErrorContext => 2);
$parser->setHandlers(Start => \&start_handler,
                     Char  => \&char_handler);
$parser->parsefile($file);
for(my $i=0;$i<=$#arr;$i++){
	print $arr[$i],"\n";
	print $hash{$i},"\n";
	print "--------\n";
}
  #6 (permalink)  
Old 07-10-2009
bhagirathi bhagirathi is offline
Registered User
  
 

Join Date: Jul 2009
Posts: 10
i did the same
Code:
awk '{ if (  $0 ~ /<cisco:cpt> | <cisco:sptp>/  )  {i++} if ( $0 ~ /<\/cisco:name>/ ) {print  "cpt=" i;i=0}}' test.xml
But the result added . so could you suggest how can it will print that differently
like
cpt= 1
sptp=2
  #7 (permalink)  
Old 07-10-2009
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,533
gawk
Code:
awk 'BEGIN{ RS="</cisco:name>"}
{
  m=split($0,a,"\n")
  f=0
  for(i=1;i<=m;i++) {
    if(a[i] ~ /<cisco:cpt>/){
        f++    
    }
  }
  print f
}' file
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 02:20 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