Perl : to split the tags from xml file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl : to split the tags from xml file
# 1  
Old 03-18-2013
Perl : to split the tags from xml file

I do have an xml sheet as below where I need the perl script to filter only the hyperlink tags.

Code:
<cols><col min="1" max="1" width="30.5703125" customWidth="1"/><col min="2" max="2" width="7.140625" bestFit="1" customWidth="1"/>
      <col min="3" max="3" width="32.28515625" bestFit="1" customWidth="1"/><col min="4" max="4" width="21" customWidth="1"/>
<c r="P1" s="2" t="s"><v>179</v></c><hyperlink ref="A88" r:id="rId1" display="Google><hyperlink ref="A89" r:id="rId1" display="http://wikipedia.org/><hyperlink ref="H123" r:id="rId225"/><hyperlink ref="E124:E125" r:id="rId226" display="suresh.reddy@gmail.com"/>

output should be
Code:
<hyperlink ref="A88" r:id="rId1" display="http://google.com/>
<hyperlink ref="A89" r:id="rId1" display="http://wikipedia.org/>

Thanks in advance....

Regards,
J
# 2  
Old 03-18-2013
Code:
$
$ cat f78
<cols><col min="1" max="1" width="30.5703125" customWidth="1"/><col min="2" max="2" width="7.140625" bestFit="1" customWidth="1"/>
      <col min="3" max="3" width="32.28515625" bestFit="1" customWidth="1"/><col min="4" max="4" width="21" customWidth="1"/>
<c r="P1" s="2" t="s"><v>179</v></c><hyperlink ref="A88" r:id="rId1" display="Google><hyperlink ref="A89" r:id="rId1" display="http://wikipedia.org/><hyperlink ref="H123" r:id="rId225"/><hyperlink ref="E124:E125" r:id="rId226" display="suresh.reddy@gmail.com"/>
$
$ perl -lne 'while(/(<hyperlink.*?>)/g) {print $1}' f78
<hyperlink ref="A88" r:id="rId1" display="Google>
<hyperlink ref="A89" r:id="rId1" display="http://wikipedia.org/>
<hyperlink ref="H123" r:id="rId225"/>
<hyperlink ref="E124:E125" r:id="rId226" display="suresh.reddy@gmail.com"/>
$
$

This User Gave Thanks to durden_tyler For This Post:
# 3  
Old 03-18-2013
Great thanks tyler...

Could you also please provide the equivalent perl script for the same.

Thanks once again...

*****************************

Also I tried the below code to get the desired output but no luck...


Code:
open(DAT, "xml.log") || die("Could not open file!");
while ( $content = <DAT> )
{
  if ($content =~ /(<hyperlink.*?>)/g)
  {
      print "$1\n";
  }
}


Last edited by scriptscript; 03-18-2013 at 12:31 PM..
# 4  
Old 03-18-2013
The equivalent perl script would be:
Code:
#! /usr/bin/perl
use warnings;
use strict;

my $line;

open DAT, "< xml.log"; # Open file handle.
while ($line = <DAT>) { # Read the file line by line
    chomp ($line); # Chomp off the new-line character
    while ($line =~ /(<hyperlink.*?>)/g) { # Loop over the line and search for all occurrences of pattern.
        print "$1\n"; # Print whatever if found.
    }
}
close DAT; # Close file handle.

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Split xml file into multiple xml based on letterID

Hi All, We need to split a large xml into multiple valid xml with same header(2lines) and footer(last line) for N number of letterId. In the example below we have first 2 lines as header and last line as footer.(They need to be in each split xml file) Header: <?xml version="1.0"... (5 Replies)
Discussion started by: vx04
5 Replies

2. Shell Programming and Scripting

Splitting xml file into several xml files using perl

Hi Everyone, I'm new here and I was checking this old post: /shell-programming-and-scripting/180669-splitting-file-into-several-smaller-files-using-perl.html (cannot paste link because of lack of points) I need to do something like this but understand very little of perl. I also check... (4 Replies)
Discussion started by: mcosta
4 Replies

3. Shell Programming and Scripting

Split XML file based on tags

Hello All , Please help me with below requirement I want to split a xml file based on tag.here is the file format <data-set> some-information </data-set> <data-set1> some-information </data-set1> <data-set2> some-information </data-set2> I want to split the above file into 3... (5 Replies)
Discussion started by: Pratik4891
5 Replies

4. Shell Programming and Scripting

Split XML file

Hi Experts, Can you please help me to split following XML file based on new Order ? Actual file is very big. I have taken few lines of it. <?xml version="1.0" encoding="utf-8" standalone="yes"?> <Orders xmlns='http://www.URL.com/Orders'> <Order> <ORDNo>450321</ORDNo> ... (3 Replies)
Discussion started by: meetmedude
3 Replies

5. Shell Programming and Scripting

How to add the multiple lines of xml tags before a particular xml tag in a file

Hi All, I'm stuck with adding multiple lines(irrespective of line number) to a file before a particular xml tag. Please help me. <A>testing_Location</A> <value>LA</value> <zone>US</zone> <B>Region</B> <value>Russia</value> <zone>Washington</zone> <C>Country</C>... (0 Replies)
Discussion started by: mjavalkar
0 Replies

6. Shell Programming and Scripting

Split xml file into many

Hi, I had a scenario need a help as I am new to this. I have a xml file employee.xml with the below content. <Organisation><employee>xxx</employee><employee>yyy</employee><employee>zzz</employee></Organisation> I want to split the file into multiple file as below. Is there a specifice way... (5 Replies)
Discussion started by: mankuar
5 Replies

7. Shell Programming and Scripting

Split a 30GB XML file into 16 pieces

I have a 30 GB XMl file which looks like this: <page> <title>APRIL</title> .........(text contents that I need to extract and store in 1.dat including the <title> tag) </page> <page> <title>August</title> ....(text contents that I need to store in 2.dat including the <title> tag) </page>... (13 Replies)
Discussion started by: shoaibjameel123
13 Replies

8. Shell Programming and Scripting

Need to split a xml file in proper format

Hi, I have a file which has xml data but all in single line Ex - <?xml version="1.0"?><User><Name>Robert</Name><Location>California</Location><Occupation>Programmer</Occupation></User> I want to split the data in proper xml format Ex- <?xml version="1.0"?> <User> <Name>Robert</Name>... (6 Replies)
Discussion started by: avishek007
6 Replies

9. Shell Programming and Scripting

Shell script to split XML file

Hi, I'm experiencing difficulty in loading an XML file to an Oracle destination table.I keep running into a memory problem due to the large size of the file. I want to split the XML file into several smaller files based on the keyword(s)/tags : '' and '' and would like to use a Unix shell... (2 Replies)
Discussion started by: bayflash27
2 Replies

10. Shell Programming and Scripting

How to parse a XML file using PERL and XML::DOm

I need to know the way. I have got parsing down some nodes. But I was unable to get the child node perfectly. If you have code please send it. It will be very useful for me. (0 Replies)
Discussion started by: girigopal
0 Replies
Login or Register to Ask a Question