grep a string in the lines between 2 strings of a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep a string in the lines between 2 strings of a file
# 1  
Old 07-10-2009
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  
Old 07-10-2009
Code:
awk '{ if (  $0 ~ /<cisco:cpt>/  )  {i++} if ( $0 ~ /<\/cisco:name>/ ) {print i;i=0}}' test.xml

# 3  
Old 07-10-2009
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  
Old 07-10-2009
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";
}

# 5  
Old 07-10-2009
Quote:
Originally Posted by bhagirathi
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)/
# 6  
Old 07-10-2009
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  
Old 07-10-2009
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Single grep to multiple strings with separate output per string

I need to grep multiple strings from a particular file. I found the use of egrep "String1|String2|String3" file.txt | wc-l Now what I'm really after is that I need to separate word count per each string found. I am trying to keep it to use the grep only 1 time. Can you guys help ? ... (9 Replies)
Discussion started by: nms
9 Replies

2. Shell Programming and Scripting

Possible to grep string based on surrounding strings?

I was wondering if it was possible to grep a pattern based on the surround text. For example, if i have an input file like this: titleA titleB titlex titleC titleD titlex titleE And I want to grep "title" and save the results only if it is not followed with a "titlex". My output... (14 Replies)
Discussion started by: jl487
14 Replies

3. Shell Programming and Scripting

Grep a string from input file and delete next three lines including the line contains string in xml

Hi, 1_strings file contains $ cat 1_strings /home/$USER/Src /home/Valid /home/Review$ cat myxml <projected value="some string" path="/home/$USER/Src"> <input 1/> <estimate value/> <somestring/> </projected> <few more lines > <projected value="some string" path="/home/$USER/check">... (4 Replies)
Discussion started by: greet_sed
4 Replies

4. Shell Programming and Scripting

Delete lines in file containing duplicate strings, keeping longer strings

The question is not as simple as the title... I have a file, it looks like this <string name="string1">RZ-LED</string> <string name="string2">2.0</string> <string name="string2">Version 2.0</string> <string name="string3">BP</string> I would like to check for duplicate entries of... (11 Replies)
Discussion started by: raidzero
11 Replies

5. Shell Programming and Scripting

replace a string with contents of a txt file containing multiple lines of strings

Hello everyone, ive been trying to replace a string "kw01" in an xml file with the contents of a txt file having multiple lines. im a unix newbie and all the sed combinations i tried resulted to being garbled. Below is the contents of the txt file: RAISEDATTIME --------------------... (13 Replies)
Discussion started by: 4dirk1
13 Replies

6. Shell Programming and Scripting

Awk - find string, search lines below string for other strings

What's the easiest way to search a file for a specific string and then look for other instances after that? I want to search for all Virtual Hosts and print out the Server Name and Document Root (if it has that info), while discarding the rest of the info. Basically my file looks like this: ...... (6 Replies)
Discussion started by: Mbohmer
6 Replies

7. UNIX for Dummies Questions & Answers

grep command to find multiple strings in multiple lines in a file.

I want to search files (basically .cc files) in /xx folder and subfolders. Those files (*.cc files) must contain #include "header.h" AND x() function. I am writing it another way to make it clear, I wanna list of *.cc files that have 'header.h' & 'x()'. They must have two strings, header.h... (2 Replies)
Discussion started by: ritikaSharma
2 Replies

8. Shell Programming and Scripting

grep a string and print strings on that line

i have a file like below ABS 12234 A C 12G CFY 23865 A C 34D i want to grep "A C" then print ABS 12G 12234 CFY 34D 23865 Thanks! (4 Replies)
Discussion started by: kingpeejay
4 Replies

9. Shell Programming and Scripting

How to get lines started with matched strings using sed or grep for loop?

I have a huge file and want to separate it into several subsets. The file looks like: C1 C2 C3 C4 ... (variable names) 1 .... 2 .... 3 .... : 22 .... 23 .... I want to separate the huge file using the column 1, which has numbers from 1 to 23 (but there are different amount of... (8 Replies)
Discussion started by: AMBER
8 Replies

10. Shell Programming and Scripting

Grep and delete lines except the lines with strings

Hi I am writing a script which should read a file and search for certain strings 'approved' or 'removed' and retain only those lines that contain the above strings. Ex: file name 'test' test: approved package waiting for approval package disapproved package removed package approved... (14 Replies)
Discussion started by: vj8436
14 Replies
Login or Register to Ask a Question