![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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>
Code:
#!/bin/sh
sed '/<\/cisco:name>/{G;}' test.xml >temp
nawk 'BEGIN{RS=""}
{
grep -c '<cisco:cpt>' print
}' temp
rm temp
in first<cisco:name> and </cisco:name> loop it should give 2 then for next it would 1 plz help |
|
||||
|
Quote:
$0 ~ /(one|two|three)/ |
|
||||
|
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";
}
|
|
||||
|
i did the same
Code:
awk '{ if ( $0 ~ /<cisco:cpt> | <cisco:sptp>/ ) {i++} if ( $0 ~ /<\/cisco:name>/ ) {print "cpt=" i;i=0}}' test.xml
like cpt= 1 sptp=2 |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|