Wow, that's a lot of code. The whole purpose of using an external library is to reduce how much code you have to write!
Try File::Slurp:
#!/usr/bin/env perl -w use strict; use File::Slurp 'edit_file_lines'; my $inFile = shift; edit_file_lines { m/\>/ && s/(\S+)\s.*/$1/ } $inFile; __END__
.... neat, huh? For an in-place edit, that's the way to go.
When I need to do something more with the lines in the file, I usually skip the full monty and go with File::Slurp::Tiny, but in this case that takes a little more coding since you have to build up an array to pass to its write_file method, and you need to create an output file:
#!/usr/bin/env perl -w use strict; use File::Slurp::Tiny qw/ read_lines write_file /; my ( $inFile, $outFile ) = @ARGV; my @keep; foreach my $line ( read_lines($inFile, chomp => 1) ) { push ( @keep, ($line =~ /\>/ ? (split(/\s/, $line))[0] : $line)); } write_file( $outFile, join("\n", @keep, '') );
In reply to Re^2: How do I modify a line within the file itself?
by 1nickt
in thread How do I modify a line within the file itself?
by newbie4659
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |