match a pattern and print the line once


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting match a pattern and print the line once
# 1  
Old 07-13-2009
match a pattern and print the line once

Hi,

I have a xml file
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:meNm>10.1.100.19</cisco:meNm>
<cisco:sptp>Cisco PortA Series</cisco:sptp>
         <cisco:aliasNameList xsi:nil="true"/>
         <cisco:owner xsi:nil="true"/>
                      <cisco:subportname>
<cisco:meNm>10.1.100.19</cisco:meNm>
<cisco:meNm>10.1.100.19</cisco:meNm>
                 <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>

i want to print once the line where it matches the pattern <cisco:cpt> like
"10.1.100.19 ".
Code:
while($0 !~ "</cisco:subname>")
{
getline;
if($0 ~ "<cisco:meNm>/") {print substr($1,13,12)}

}' *.xml

then it print

"10.1.100.19" 4 times. So how can i print only once. Plz suggest

and this series is repeatable and the search the pattern till it find </cisco:subname>

Last edited by bhagirathi; 07-13-2009 at 10:29 AM..
# 2  
Old 07-13-2009
Try:

Code:
awk '/cisco:subname/,/\/cisco:subname/ { if ($0 ~ /cisco:meNm/) { print substr($0,13,index($0,"</")-13); exit; } } ' filename

# 3  
Old 07-13-2009
hi,

Actually my code is like
Code:
awk '/<cisco:name>/ {
cpt=0
sptp=0
while($0 !~ "</cisco:name>")
{
getline;
if($0 ~ "<cisco:meNm>") {print substr($1,13,11)}
if($0 ~ "<cisco:cpt>") cpt++;
if($0 ~ "<cisco:sptp>") sptp++;
}
print "cpt="cpt
print "sptp="sptp }' test.xml

and output comes as
Code:
10.1.100.19
10.1.100.19
10.1.100.19
10.1.100.19
cpt=2
sptp=2
10.1.100.20
cpt=1
sptp=1

But i need as
Code:
10.1.100.19
cpt=2
sptp=2
10.1.100.20
cpt=1
sptp=1



---------- Post updated at 08:08 AM ---------- Previous update was at 08:05 AM ----------

Quote:
Originally Posted by dennis.jacob
Try:

Code:
awk '/cisco:subname/,/\/cisco:subname/ { if ($0 ~ /cisco:meNm/) { print substr($0,13,index($0,"</")-13); exit; } } ' filename

hi Jacob,

this individual code works but i have few if condition below . so after exit it wont go for next condition
# 4  
Old 07-13-2009
Your code looks familiar to me. Smilie Change the below line...
Code:
if($0 ~ "<cisco:meNm>") {S=substr($1,13,11); if(S!=prevS && S!="") {print S;S=prevS}}
and type 
prevS="" after cpt=0

I could not verify this code, so please look for { } and ()
# 5  
Old 07-13-2009
You can simply pipe your ouput to "uniq" and it would be fine !
# 6  
Old 07-13-2009
Quote:
Originally Posted by rakeshawasthi
Your code looks familiar to me. Smilie Change the below line...
Code:
if($0 ~ "<cisco:meNm>") {S=substr($1,13,11); if(S!=prevS && S!="") {print S;S=prevS}}
and type 
prevS="" after cpt=0

I could not verify this code, so please look for { } and ()
still i am getting the same result
Code:
10.1.100.19
10.1.100.19
10.1.100.19
10.1.100.19
cpt=2
sptp=2
10.1.100.20
cpt=1
sptp=1

# 7  
Old 07-13-2009
Ok, let me check,
Why I am not suggesting you uniq or sort, is because it may strip other lines too.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match Pattern and print pattern and multiple lines into one line

Hello Experts , require help . See below output: File inputs ------------------------------------------ Server Host = mike id rl images allocated last updated density vimages expiration last read <------- STATUS ------->... (4 Replies)
Discussion started by: tigerhills
4 Replies

2. Shell Programming and Scripting

Regex: print matched line and exact pattern match

Hi experts, I have a file with regexes which is used for automatic searches on several files (40+ GB). To do some postprocessing with the grep result I need the matching line as well as the match itself. I know that the latter could be achieved with grep's -o option. But I'm not aware of a... (2 Replies)
Discussion started by: stresing
2 Replies

3. Shell Programming and Scripting

Match pattern and print the line number of occurence using awk

Hi, I have a simple problem but i guess stupid enough to figure it out. i have thousands rows of data. and i need to find match patterns of two columns and print the number of rows. for example: inputfile abd abp 123 abc abc 325 ndc ndc 451 mjk lkj... (3 Replies)
Discussion started by: redse171
3 Replies

4. Shell Programming and Scripting

Multiple pattern match and print the output in a single line

I need to match two patterns in a log file and need to get the next line of the one of the pattern (out of two patterns) that is matched, finally need to print these three values in a single line. Sample Log: 2013/06/11 14:29:04 <0999> (725102) Processing batch 02_1231324 2013/06/11... (4 Replies)
Discussion started by: rpm120
4 Replies

5. Shell Programming and Scripting

Print only next pattern in a line after a pattern match

I have 2013-06-11 23:55:14 1Umexd-0004cm-IG <= user@domain.com I need sed/awk operation on this, so that it should print the very next pattern only after the the pattern mach <= ie only print user@domain.com (7 Replies)
Discussion started by: anil510
7 Replies

6. Shell Programming and Scripting

awk print pattern match line and following lines

Data: Pattern Data Data Data Data Data Data Data Data Data ... With awk, how do I print the pattern matching line, then the subsequent lines following the pattern matching line. Varying number of lines following the pattern matching line. (9 Replies)
Discussion started by: dmesserly
9 Replies

7. Shell Programming and Scripting

Print Line if next line Match a pattern

Hi All, Does anyone know how to print 1H1A....... in peal script print line ^1H1A....... if next line equal 5R0RECEIPT.... Thank for help:D Cat st.txt 1H1A-IN-11-5410-0009420|1010047766|dsds|1|N|IN|IN|000000|1||N|<<<line match 5R0RECEIPT| 5R0RECEIPT|... (2 Replies)
Discussion started by: kittiwas
2 Replies

8. UNIX for Dummies Questions & Answers

MATCH A PATTERN AND PRINT A LINE ABOVE AND BELOW

Dear All, Hv a very specific requirement. I have a very large text file and in which I have to match a pattern and insert a line above and below. Eg: My file cat test date1 date2 date3 date4 I need to match 'date3' and insert "Reminder1" above date3 and insert 'reminder2'... (4 Replies)
Discussion started by: gokulj
4 Replies

9. Shell Programming and Scripting

Search word in a line and print earlier pattern match

Hi All, I have almost 1000+ files and I want to search specific pattern. Looking forwarded your input. Search for: word1.word2 (Which procedure contain this word, I need procedure name in output. Expected output: procedure test1 procedure test2 procedure test3 procedure test4 ... (7 Replies)
Discussion started by: susau_79
7 Replies

10. Shell Programming and Scripting

match a pattern, print it and the next line

I have a file nbu_faq.txt (Question/answer) which looks like this What I am trying to do is write out each question in a file1.txt and than the question/answer in a file2.txt like this file1.txt Q: What is nbu? Q: What is blablabla...? Q: Why ....? file2.txt Q: What is nbu? A:... (4 Replies)
Discussion started by: nymus7
4 Replies
Login or Register to Ask a Question