Need to modify several XML files with Perl script in Linux env -
i have several xml files named tc_circle1
, tc_circle2
, `tc_point1, etc in directory , want use script update start , stop dates in each file. start , stop dates inside , tags in each file.
i had script worked when using sun machines not working on new hp linux machines. doesn't show errors , doesn't change dates. need getting work in linux. script:
#!/usr/local/bin/perl $numargs = @argv; if ($numargs != 2) { print "usage: replace_default_date.pl default_start_date default_stop_date\n"; } @filenames = `ls tc*`; chomp(@filenames); foreach $file (@filenames) { open(regfile, "$file") || die "cannot open |$file|"; @lines = <regfile>; close(regfile); open(writefile), ">$file") || die "cannot open |$file|"; foreach $line (@lines) { if ($line =~ /default_start_date/) { $newline = " " . $argv[0]; print writefile "$newline\n"; } elsif ($line =~ /default_stop_date/) { $newline = " " . $argv[1]; print writefile "$newline\n"; } else { print writefile "$line\n"; } } close (writefile); }
here's how files modified @ beginning:
<requestsomething xmlns="http://something.com/accessservice"> <period xmlns=""> <start>2013-03-06t00:00:00</start> <stop>2013-03-07t00:00:00</stop> </period> ... rest of xml file... </requestsomething>
thanks in advance, crystal
there several problems script.
1) there compile error because of closing parenthesis:
open(writefile), ">$file") || die "cannot open |$file|";
should writting
open(writefile, ">$file") || die "cannot open |$file|";
2) should use backticks instead of single quotes in
@filenames = 'ls tc*';
otherwise @filenames
contain string 'ls tc*' instead of actual list of filenames:
@filenames = `ls tc*`;
3) sure path perl interpreter /usr/local/bin/perl
? (try which perl
command line check path). if not first line should changed.
4) script never work on xml data showed since designed replace lines contain strings default_start_date , default_stop_date (with dates provided arguments script). these strings not appear in data showed us.
however, script work if xml file this:
<requestsomething xmlns="http://something.com/accessservice"> <period xmlns=""> <start> default_start_date </start> <stop> default_stop_date </stop> </period> ... rest of xml file... </requestsomething>
i hope work, anyway recommend rewrite script because uses unreliable , dangerous way of changing xml files.
Comments
Post a Comment