Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl problems printing output to a new file

Tags:

perl

I want to remove all lines in a text file that start with HPL_ I have acheived this and can print to screen, but when I try to write to a file, I just get the last line of the amended text printed in the new file. Any help please!

open(FILE,"<myfile.txt"); 
@LINES = <FILE>; 
close(FILE); 
open(FILE,">myfile.txt"); 
foreach $LINE (@LINES) { 
@array = split(/\:/,$LINE); 


my $file = "changed";

open OUTFILE, ">$file" or die "unable to open $file $!";

print OUTFILE $LINE unless ($array[0] eq "HPL_");

} 
close(FILE); 
close (OUTFILE);




exit;
like image 990
James_up_North Avatar asked Dec 06 '22 21:12

James_up_North


1 Answers

You just want to remove all lines that start with HPL_? That's easy!

perl -pi -e 's/^HPL_.*//s' myfile.txt

Yes, it really is just a one-liner. :-)

like image 132
Chris Jester-Young Avatar answered Jan 10 '23 14:01

Chris Jester-Young