Re: newbie question about removing un needed extra lines

Discussion in 'Perl' started by Scott Taylor, Aug 3, 2003.

  1. Scott Taylor

    Scott Taylor Guest

    Eric J. Roode wrote:
    > Scott Taylor <> wrote in
    > news:bggslc$686$:
    >
    >
    >>I've just started learning perl, and had two questions. One, what is
    >>the best way to open a text file and remove all of the empty lines?
    >>Example:
    >>
    >>
    >>
    >>
    >>
    >>
    >>
    >>
    >>should turn into
    >>
    >>
    >>
    >>
    >>

    >
    >
    > perl -ne '/\S/&&print' file >file
    >


    Well, I was essentailly doing the same thing with this code:

    use warnings;
    use strict;

    open (DATA, "+<db.db") or die "Can't open data!";
    open (UPDATED_DATA, "+<db2.db") or die "Can't open data!";

    while (<DATA>) {
    next if /^$/;
    print UPDATED_DATA $_;
    }

    close (DATA);
    close (UPDATED_DATA);


    But how could I then copy the UPDATED_DATA file back to the DATA file
    and delete the UPDATED_DATA file? Or is there an easier way with out
    ever needing to create a secondary temporary file?

    Thanks,

    Scott Taylor
    Scott Taylor, Aug 3, 2003
    #1
    1. Advertising

  2. Scott Taylor

    Simon Taylor Guest

    Hello Scott,

    Look into the -i flag. It allows you to edit files in place, (and
    optionally to create a backup file).

    For example:

    perl -pi -e 's/abc/XYZ/' file.txt

    will overwrite the original 'file.txt' file with the edited file.

    See perldoc perlrun for more info.

    Regards,

    Simon Taylor

    >> perl -ne '/\S/&&print' file >file
    >>

    >
    > Well, I was essentailly doing the same thing with this code:
    >
    > use warnings;
    > use strict;
    >
    > open (DATA, "+<db.db") or die "Can't open data!";
    > open (UPDATED_DATA, "+<db2.db") or die "Can't open data!";
    >
    > while (<DATA>) {
    > next if /^$/;
    > print UPDATED_DATA $_;
    > }
    >
    > close (DATA);
    > close (UPDATED_DATA);
    >
    >
    > But how could I then copy the UPDATED_DATA file back to the DATA file
    > and delete the UPDATED_DATA file? Or is there an easier way with out
    > ever needing to create a secondary temporary file?
    Simon Taylor, Aug 4, 2003
    #2
    1. Advertising

  3. "Eric J. Roode" <> wrote in
    news:Xns93CCBD6E747E7sdn.comcast@206.127.4.25:

    > unite @lines;


    er, "untie" of course.

    --
    Eric
    $_ = reverse sort qw p ekca lre Js reh ts
    p, $/.r, map $_.$", qw e p h tona e; print
    Eric J. Roode, Aug 4, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ned D Hanks
    Replies:
    0
    Views:
    918
    Ned D Hanks
    Aug 4, 2003
  2. Andy Fish
    Replies:
    1
    Views:
    1,439
    Andy Fish
    Dec 21, 2004
  3. mathieu
    Replies:
    3
    Views:
    595
    Bo Persson
    Sep 4, 2009
  4. OccasionalFlyer

    Removing Extra Table Cell

    OccasionalFlyer, Feb 28, 2011, in forum: HTML
    Replies:
    22
    Views:
    949
    Jonathan N. Little
    Mar 2, 2011
  5. Siobhan

    Removing extra information from Soap Exception

    Siobhan, Feb 19, 2005, in forum: ASP .Net Web Services
    Replies:
    3
    Views:
    179
    Guest
    Feb 23, 2005
Loading...

Share This Page