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

<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

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

awk '{ if (  $0 ~ /<cisco:cpt>/  )  {i++} if ( $0 ~ /<\/cisco:name>/ ) {print i;i=0}}' test.xml

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.

more formally, perl can handle XML document with many availiable module.

You may try below to see the result

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";
}

link them with OR, something like this:
$0 ~ /(one|two|three)/

i did the same

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

gawk

awk 'BEGIN{ RS="</cisco:name>"}
{
  m=split($0,a,"\n")
  f=0
  for(i=1;i<=m;i++) {
    if(a ~ /<cisco:cpt>/){
        f++    
    }
  }
  print f
}' file

hi ghostdog,

it gives the output as
ctp=0
ctp=0
.
..
53 times

Try this...

awk '/<cisco:name>/ {
   cpt=0
   sptp=0
   while($0 !~ "</cisco:name>")
   {
      getline;
      if($0 ~ "<cisco:cpt>") cpt++;
      if($0 ~ "<cisco:sptp>") sptp++;
   }
   print "cpt="cpt
   print "sptp="sptp
}' input

Adding more conditions over this should be easy for you.