Reading and writing line by line from / to the same file

I am working with xml files that I need to manipulate in my script. My first approach to this was:

qx(perl export_xml.pl $export_params > $path$prefix\investment.xml); # Create the xml-file open DERI, '+<'.$path.$prefix.'investment.xml' or die 'Can\'t open investment.xml: '.$!; my @derivative_xml = <DERI>; seek(DERI, 0, 0); foreach (@derivative_xml) { $_ =~ s/^\s*$//g; $_ =~ s/^.*detected on Server.*$//g; $_ = encode('utf8', $_); } print DERI join('', @derivative_xml); 

This works for testing, but unfortunately the real files are too large for that (up to 6 GB).

Is there a way to read a file line by line and then change the input through a file descriptor? Sort of

 foreach (<DERI>) { #@derivative_xml) { $_ =~ s/^\s*$//g; $_ =~ s/^.*detected on Server.*$//g; $_ = encode('utf8', $_); } 

I can’t verify this in an absurd time, so it would be nice if I didn’t have trial and error here.

Thanks in advance!

+4
source share
1 answer

That should work. No need for another script file.

 perl -pi -e 's/^\s*$//g;s/^.*detected on Server.*$//g;$_ = encode('utf8', $_)' investment.xml 

Not tested, although with a huge file up to 6 GB. Check this out and see how long it takes.

0
source

Source: https://habr.com/ru/post/1481174/


All Articles