0

Hi I want to call the xml file and I want to change the tag name. here sample coding of get txt file.

my $inputtxt=$ARGV[0];

open(TXT,"$inputtxt"); my $txtstr=join("",<TXT>); close TXT;
$txtstr =~ s#\n\n#\n#sgi;

print $txtstr;

open (OUT,">$inputtxt.txt"); print OUT $txtstr; close (OUT);

I need how to call xml file, in the above script. Please help me.

3
Contributors
2
Replies
22
Views
3 Years
Discussion Span
Last Post by venyz
0

Hi hasvi,

I need how to call xml file, in the above script. Please help me.

How do you mean call xml file? Please don't try to parse xml file using regex, you can use modules on metaCPAN or CPAN such as XML::Simple for what you are trying to do. See the link I pointed to.

That been said, there are somethings not ok in your above code:
1. Always use these pragma:
use warnings;
use strict;
In your perl codes, you will be glad you do.

  1. Use 3 arugments open option i.e open( my $fh, '<', $filename) or die "can't open file: $!;"

  2. Use lexical filehandle like I used it in point 2, not bareword like you used TXT

  3. Then test if your file opens, or fails, just like I did in 2 above. Or you can use autodie pragma.

Hope this helps

Edited by 2teez

0

What you need can actually be done using a one-liner.

perl -i.bak -pe 's/\n\n/\n/sg' inputtxt

This would edit inputtxt in place and leave the original with a .bak file extension.
The /i regex switch is not needed, as you are only working with newline characters.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.