in reply to How do I modify a line within the file itself?

So Path::Tiny it like autodies for you :)

#!/usr/bin/perl -- use strict; use warnings; use Path::Tiny; my( $inFile, $outFile ) = @ARGV; my $outfh = path( $outFile )->openw_raw; my $infh = path( $infile )->openr_raw; while( my $line = <$infh>){ chomp $line; if ($line =~ /\>/) { my @modify_line = split(/\s/,$line); $line = $modify_line[0]; } print $outfh "$line\n"; } close $infh; close $outfh;

Replies are listed 'Best First'.
Re^2: How do I modify a line within the file itself?
by 1nickt (Canon) on Jul 09, 2015 at 02:18 UTC

    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, '') );
    Remember: Ne dederis in spiritu molere illegitimi!

      I thank you for the examples

      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!

      Didn't you notice the reduction? There is no 3 argument open , no "or die" verbosity? I think thats neat

      ... edit_file_lines .... neat, huh? For an in-place edit, that's the way to go.

      Did you know that reads the whole file into memory? It limits the size of files you can edit to the amount of RAM you have (and perl has access to)

      That /\>/ hints to me the OP might be dealing with gigabyte sized files, probably a good idea to avoid slurping

      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:

      That has the same issue, it read whole file into memory/an array, duplicate this array, write it out ; Too much memory requirement based on size of file.

        Did you know that reads the whole file into memory? It limits the size of files you can edit to the amount of RAM you have (and perl has access to)

        Yep, that's what slurping a file is ...

        That /\>/ hints to me the OP might be dealing with gigabyte sized files, probably a good idea to avoid slurping

        Or, maybe a 20-line conf file of some sort. Or anything. 99% of all files that are read are not of gigabyte size, I reckon, and new perlers are often needlessly steered away from using existing modules by more experienced programmers who are thinking about edge cases.

        Remember: Ne dederis in spiritu molere illegitimi!