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!

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.