Print xml if value matches


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print xml if value matches
# 1  
Old 06-10-2010
Print xml if value matches

Hello Gurus....

Can any one please let me know how to print entire xml if the vlaue in the xml matches to a variable given. In PERL.

example:

<rpad:systemId>abc</hcdd:systemId>
<rpad:hcdd>1172</hcdd:hcdid>
</get:Request></soapenv:Body></soapenv:Envelope>

if value macthes 1172 I need to print entire xml or else ignore thaht xml, and check for the next xml.

actually all these xmls are in a log file.
# 2  
Old 06-10-2010
Quote:
Originally Posted by thankful123
...
Can any one please let me know how to print entire xml if the vlaue in the xml matches to a variable given. In PERL.

example:

<rpad:systemId>abc</hcdd:systemId>
<rpad:hcdd>1172</hcdd:hcdid>
</get:Request></soapenv:Body></soapenv:Envelope>

if value macthes 1172 I need to print entire xml or else ignore thaht xml, and check for the next xml.

...
Do you mean print the complete XML tag ?
Or print the entire XML file ?

tyler_durden
# 3  
Old 06-10-2010
I mean to print complete xml file not just the tag
# 4  
Old 06-10-2010
Maybe something like this ?

Code:
$ 
$ 
$ # list the xml files in current directory
$ ls -1 *.xml
file1.xml
file2.xml
file3.xml
$ 
$ # display the content of each xml file
$ cat -n file1.xml
     1    <?xml version="1.0"?>
     2    <catalog>
     3       <book id="bk101">
     4          <title>XML Developer's Guide</title>
     5          <genre>Computer</genre>
     6          <price>44.95</price>
     7       </book>
     8       <book id="bk102">
     9          <title>Midnight Rain</title>
    10          <genre>Fantasy</genre>
    11          <price>5.95</price>
    12       </book>
    13    </catalog>
$ 
$ cat -n file2.xml
     1    <?xml version="1.0"?>
     2    <catalog>
     3       <book id="bk103">
     4          <title>Maeve Ascendant</title>
     5          <genre>Fantasy</genre>
     6          <price>24.99</price>
     7       </book>
     8       <book id="bk104">
     9          <title>Oberon's Legacy</title>
    10          <genre>Fantasy</genre>
    11          <price>29.99</price>
    12       </book>
    13    </catalog>
$ 
$ cat -n file3.xml
     1    <?xml version="1.0"?>
     2    <catalog>
     3       <book id="bk105">
     4          <title>The Sundered Grail</title>
     5          <genre>Fantasy</genre>
     6          <price>5.95</price>
     7       </book>
     8       <book id="bk106">
     9          <title>Lover Birds</title>
    10          <genre>Romance</genre>
    11          <price>4.95</price>
    12       </book>
    13    </catalog>
$ 
$ # display the content of the Perl program
$ # the inline comments should be self-explanatory
$ cat -n searchandprint.pl
     1    #!/usr/bin/perl -w
     2    # Usage: perl searchandprint.pl <value> <directory>
     3    $search = $ARGV[0];
     4    # if directory is specified then search that,
     5    # otherwise look in the current directory
     6    if (defined $ARGV[1]) {$pattern = $ARGV[1]."/*.xml"}
     7    else {$pattern = "*.xml"}
     8    $found = 0;
     9    while (defined ($file = glob $pattern)) {
    10      print "Checking $file ... \t";
    11      open (F, $file) or die "Can't open $file: $!";
    12      while (<F>) {
    13        if ($found) {print}
    14        elsif (/$search/) {
    15          print "found the value; now printing the file...\n","="x80,"\n";
    16          # print the array; flush;
    17          # print current line; set $found to 1
    18          foreach (@lines) {print}
    19          @lines = ();
    20          print;
    21          $found = 1;
    22        } else {
    23          push @lines, $_;
    24        }
    25      }
    26      close (F) or die "Can't close $file: $!";
    27      @lines = ();
    28      if ($found == 0) {print "nothing here; moving on...\n"}
    29      else {$found = 0}
    30      print "="x80,"\n";
    31    }
$ 
$ # now run the Perl program
$ perl searchandprint.pl 5.95
Checking file1.xml ...     found the value; now printing the file...
================================================================================
<?xml version="1.0"?>
<catalog>
   <book id="bk101">
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
   </book>
   <book id="bk102">
      <title>Midnight Rain</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
   </book>
</catalog>
================================================================================
Checking file2.xml ...     nothing here; moving on...
================================================================================
Checking file3.xml ...     found the value; now printing the file...
================================================================================
<?xml version="1.0"?>
<catalog>
   <book id="bk105">
      <title>The Sundered Grail</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
   </book>
   <book id="bk106">
      <title>Lover Birds</title>
      <genre>Romance</genre>
      <price>4.95</price>
   </book>
</catalog>
================================================================================
$ 
$

tyler_durden

---------- Post updated at 07:17 PM ---------- Previous update was at 07:12 PM ----------

On second thoughts, Perl seems to be overkill for this type of job. You could cook up a quick and dirty shell script in a fraction of the time it'll take you to write that Perl program -

Code:
$ 
$ 
$ for i in *.xml; do
>   echo "Checking file => $i..."
>   grep 5.95 $i 1>/dev/null 2>&1
>   if [ $? -eq 0 ]; then
>     cat $i
>   fi
> done
Checking file => file1.xml...
<?xml version="1.0"?>
<catalog>
   <book id="bk101">
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
   </book>
   <book id="bk102">
      <title>Midnight Rain</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
   </book>
</catalog>
Checking file => file2.xml...
Checking file => file3.xml...
<?xml version="1.0"?>
<catalog>
   <book id="bk105">
      <title>The Sundered Grail</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
   </book>
   <book id="bk106">
      <title>Lover Birds</title>
      <genre>Romance</genre>
      <price>4.95</price>
   </book>
</catalog>
$ 
$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print whole line if variables matches

Der colleagues, 4 days I am trying to solve my issue and no success.. Maybe you can give me a clue how to achieve what I need.. So I have two files. file1 example: 1_column1.1 1_column2.1 aaa 1_column4.1 1_column1.2 1_column2.2 ttt 1_column4.2 1_column1.3 1_column2.3 ... (10 Replies)
Discussion started by: nypreH
10 Replies

2. Shell Programming and Scripting

How to print line if two lines above it matches patterns.?

Hi, I could only find examples to print line before/after a match, but I'd need to print line after two separate lines matching. E.g.: From the below log entry, I would need to print out the 1234. This is from a huge log file, that has a lot of entries with "CLIENT" and "No" entries (+ other... (3 Replies)
Discussion started by: Juha
3 Replies

3. UNIX for Dummies Questions & Answers

Print Matches to New Columns

Hi all, I have a problem that I'm struggling to resolve. I have two files that look like this: File 1 654654654 3 987987987 2 321321321 1 File 2 14NS0064 654654654 14NS0054 654654654 14NS0032 654654654 14NS0090 987987987 14NS0093 987987987 14NS0056 321321321 As you may notice,... (2 Replies)
Discussion started by: winkleman
2 Replies

4. UNIX for Dummies Questions & Answers

Print only '+' or '-' if string matches (two files)

I would like to add two additional conditions to the actual code I have: print '+' if in File2 field 5 is greater than 35 and also field 7 is grater than 90. while read -r line do grep -q "$line" File2.txt && echo "$line +" || echo "$line -" done < File1.txt ' Input file 1: ... (5 Replies)
Discussion started by: bernardo.bello
5 Replies

5. Shell Programming and Scripting

Compare 2 files and print matches and non-matches in separate files

Hi all, I have two files, chap.txt and complex.txt. chap.txt looks like this: a d l m r k complex.txt looks like this: a c d e l m n j a d l p q r c p r m ......... (7 Replies)
Discussion started by: AshwaniSharma09
7 Replies

6. UNIX for Dummies Questions & Answers

Print only matches

Im not sure how to tell this awk command to only print when there is a match between the two files. Right now its printing the words in quotation even if there is not a match. I tried removing the additional info and it prints blank spaces?? awk ' { IP = $1 $1 = "" } FNR == NR { ... (2 Replies)
Discussion started by: sectech
2 Replies

7. Shell Programming and Scripting

Compare two text files and print matches

Hi, I am looking for a way to compare two text files and print the matches. For example; File1.txt 89473036 78474384 48948408 95754748 47849030 File2.txt 47849030 46730356 16734947 78474384 36340047 Output: (11 Replies)
Discussion started by: lewk
11 Replies

8. UNIX for Advanced & Expert Users

Print line if subsrt matches array

Hi Folks! im printing all lines where the characters in position 270-271 match 33|H1|HA|KA|26 so i came up with this #!/bin/bash array=(33 H1 HA KA 26 ) for i in "${array}" do #echo $i awk '{ if (substr($0,270,2)~'/$i/') print; }' $1 >> $1.temp done It works fine . but... (2 Replies)
Discussion started by: phpsnook
2 Replies

9. Shell Programming and Scripting

How to print line if field matches?

Hi all, I got several lines line this a b c d e 1 e a 1 c d e 3 f a b c 1 e 8 h a b c d e 1 w a 1 c d e 2 w a b c d e 1 t a b c d e 7 4 How can I print the line if 1 is the field one before the last field? Basicly this 2 field ? a b c d e 1 e a b c d e 1 t The file I got is... (7 Replies)
Discussion started by: stinkefisch
7 Replies

10. Shell Programming and Scripting

print next line if matches a particular word..need help

Hi i need a help for making a script whch can print next line if it matches a particular word like file1 have ename Mohan eid 2008 ename Shyam eid 345 if scipt got Mohan it will print next line (eid 2008) pls help me .......:) (2 Replies)
Discussion started by: anish19
2 Replies
Login or Register to Ask a Question