in reply to placing an existing line from my XML file to the top of the file

Are you wanting to add the last line to the top and get rid of it at the bottom? Or just add last line to the top and keep rest of the file as is? I suggest you make a backup on you own before you try using this (if you do use it)

This will leave the last line in the file while putting it at the beginning as well:

use strict; use warnings; my @array; open( my $orig, '<', 'original_file' ); open( my $backup, '+>', 'original_backup' ); while (<$orig>) { push ( @array, $_ ); print( $backup $_ ); } close($orig); open( my $new_orig, '+>', 'original_file' ); print( $new_orig $array[-1] ); print( $new_orig @array ); close( $new_orig );

This will copy the last line to the beginning while removing it from the end

use strict; use warnings; my @array; open( my $orig, '<', 'original_file' ); open( my $backup, '+>', 'original_backup' ); while (<$orig>) { push( @array, $_ ); print( $backup $_ ); } close($orig); open ( my $new_orig, '+>', 'original_file' ); print( $new_orig pop @array ); print( $new_orig @array ); close ( $new_orig );

Sample input data:

is a test!!! this

Replies are listed 'Best First'.
Re^2: placing an existing line from my XML file to the top of the file
by gasjunkie_jabz (Novice) on Aug 20, 2015 at 22:08 UTC

    Thank you James! the code for "copying the last line to the beginning while removing it from the end" was exactly what I needed my script to do.

      I wouldnt use that for very large file btw.

        Hi James, what would you suggest for large files of 500+ lines of results in 1 XML?