LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-16-2008, 12:34 PM   #1
ruffles
LQ Newbie
 
Registered: Apr 2007
Location: usually in front of the keyboard
Distribution: Redhat, Fedora, Centos, Ubuntu, Debian, Solaris
Posts: 15

Rep: Reputation: 0
Question perl replace script for xml


I'm trying to replace ocurrence of xml fragment in a large xml file.

replacement from:

Code:
$what= <<ENDHTML;

<image type="filmposter" id="$rd">
<imagefile type="tiny">
<filetype>jpg</filetype>
<filedate>DATA_ZDJ</filedate>
<filesize>ROZMIAR</filesize>
<location>images/1320521STARTtiny1.jpg</location>
<width>SZEROKOSC</width>
<height>WYSOKOSC</height>
</imagefile>
<imagefile type="thumb">
<filetype>jpg</filetype>
<filedate>DATA_ZDJ</filedate>
<filesize>ROZMIAR</filesize>
<location>images/1320521STARTtiny1.jpg</location>
<width>SZEROKOSC</width>
<height>WYSOKOSC</height>
</imagefile>
<imagefile type="photo">
<filetype>jpg</filetype>
<filedate>DATA_ZDJ</filedate>
<filesize>ROZMIAR</filesize>
<location>images/1320521STARTphoto.jpg</location>
<width>SZEROKOSC</width>
<height>WYSOKOSC</height>
</imagefile>
</image>
ENDHTML
to:

Code:
$whatfor= <<ENDHTML;

<image type="filmposter" id="$rd">
<imagefile type="tiny">
<filetype>jpg</filetype>
<filedate>$ARGV[1]</filedate>
<filesize>$ARGV[2]</filesize>
<location>images/1320521STARTtiny1.jpg</location>
<width>$ARGV[3]</width>
<height>$ARGV[4]</height>
</imagefile>
<imagefile type="thumb">
<filetype>jpg</filetype>
<filedate>$ARGV[1]</filedate>
<filesize>$ARGV[2]</filesize>
<location>images/1320521STARTtiny1.jpg</location>
<width>$ARGV[3]</width>
<height>$ARGV[4]</height>
</imagefile>
<imagefile type="photo">
<filetype>jpg</filetype>
<filedate>$ARGV[1]</filedate>
<filesize>$ARGV[5]</filesize>
<location>images/1320521STARTphoto.jpg</location>
<width>$ARGV[6]</width>
<height>$ARGV[7]</height>
</imagefile>
</image>
ENDHTML
So far I've tried:

Code:
#! /usr/bin/perl -w

##insert above variables here##

$file = '/path/to/file/file.xml';
$file =~ s/$what/$whatfor/g
and

Code:
#! /usr/bin/perl -w

##insert above variables here##

perl -pi -e "s/$what/$whatfor/e" '/path/to/file/file.xml'

None of them worked, of course, since this is my first perl script.

Could you point me in the right direction?
 
Old 06-16-2008, 04:33 PM   #2
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 70
If you want to pursue the first usage, you will have to open and overwrite the file yourself. If you want to use the second usage, just put the entire command as an argument to system.
 
Old 06-16-2008, 04:42 PM   #3
ruffles
LQ Newbie
 
Registered: Apr 2007
Location: usually in front of the keyboard
Distribution: Redhat, Fedora, Centos, Ubuntu, Debian, Solaris
Posts: 15

Original Poster
Rep: Reputation: 0
As for the first one, well, I actually don't really know how to do that. Did some googling, found a few sample scripts, but they didn't work for me.
As for the second, I tried it and it worked ( with simple replacements ), but i don't know how to pass a variable from shell (sh) to perl command ( it didn't work when I just placed the whole string in the command line )
 
Old 06-16-2008, 10:26 PM   #4
sundialsvcs
Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 5,354

Rep: Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105
Perl has many CPAN-modules for manipulating XML. These include libraries specifically designed to do XML search-and-replace, e.g. through XPath-expressions.

Check out, for example, XML::Twig.

A CPAN-search for XML will produce a huge number of entries. This is a great example of a situation where "automatically dropping into procedural-programming mode... 'how do I write a program to do this?'" will instantly be the wrong approach. The answer is "you don't." The program has already been written. Searching through and transforming an XML structure can be done declaratively, using the facilities provided by the CPAN modules.
 
Old 06-17-2008, 12:17 AM   #5
ruffles
LQ Newbie
 
Registered: Apr 2007
Location: usually in front of the keyboard
Distribution: Redhat, Fedora, Centos, Ubuntu, Debian, Solaris
Posts: 15

Original Poster
Rep: Reputation: 0
Problem is that i don't have control over modules installed on the server and there are no xml related modules already installed.
Also, time is an issue here as well.
I just need to replace one with the other, and sed is throwing errors about suffix larger than 512

Last edited by ruffles; 06-17-2008 at 12:23 AM.
 
Old 06-17-2008, 12:39 AM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
here's an awk script. you can convert that to Perl.
Code:
awk '
/filedate|filesize|width|height/ {
  gsub(/>.*</,">$ARGV["++counter "]<")
  print;next
 }1' file
 
Old 06-17-2008, 01:58 AM   #7
ruffles
LQ Newbie
 
Registered: Apr 2007
Location: usually in front of the keyboard
Distribution: Redhat, Fedora, Centos, Ubuntu, Debian, Solaris
Posts: 15

Original Poster
Rep: Reputation: 0
this script should find occurence of string depending on $rd variable
<image type="filmposter" id="$rd">
it's called from loop within .sh script

I also tried this:

open(FILE,"</path/to/file/file.xml");
while(<FILE>){
$_=~s/$what/$whatfor/;
push @new_file,$_;
}
close(FILE);
open(FILE,">/path/to/file/file1.xml");
for $i(0..$#new_file){
print FILE $new_file[$i];
}
close(FILE);

but that didn't work either
 
Old 06-17-2008, 05:39 AM   #8
ruffles
LQ Newbie
 
Registered: Apr 2007
Location: usually in front of the keyboard
Distribution: Redhat, Fedora, Centos, Ubuntu, Debian, Solaris
Posts: 15

Original Poster
Rep: Reputation: 0
solved

I solved this problem and I'll try to post the answer for later generations later on
 
Old 06-17-2008, 05:30 PM   #9
ruffles
LQ Newbie
 
Registered: Apr 2007
Location: usually in front of the keyboard
Distribution: Redhat, Fedora, Centos, Ubuntu, Debian, Solaris
Posts: 15

Original Poster
Rep: Reputation: 0
Well, here you go, maybe it'll help someone:

file change.pl:
Code:
#!/usr/local/bin/perl

$what="<image type=\"filmposter\" id=\"" . $ARGV[0] . "_pic\"\/>";

while (<STDIN>) {
     s/$what/<image type="filmposter" id="$ARGV[0]_pic">\n<imagefile type="tiny">\n<filedate>$ARGV[1]<\/filedate>~AND_SO_ON~<\/image>/;
        print;
and
chmod a+x change.pl

within sh script:
Code:
cd /dir/with/change.pl/script/
cat /path/to/file/file.xml | ./change.pl $a $d > /path/to/file/file1.xml
mv /path/to/file/file1.xml /path/to/file/file.xml
(if you want to change all occurences of a string, change
<\/image>/;
to
<\/image>/g;
in change.pl)

Thanks for your help.

Last edited by ruffles; 06-17-2008 at 05:34 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
checking for XML::Parser... configure: error: XML::Parser perl module is required for kornerr Linux - General 11 11-16-2008 07:24 AM
Transforming XML to HTML: how do I replace &apos; with ' vasudevadas Programming 3 08-20-2007 09:41 AM
XML and Perl msvinaykumar Programming 3 08-08-2006 08:16 AM
Converting a Windows Perl script to a Linux Perl script. rubbercash Programming 2 07-19-2004 10:22 AM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 01:04 AM


All times are GMT -5. The time now is 05:25 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration