Professional Web Applications Themes

Delete multiple blank lines - PERL Beginners

> Hi all, I could use some help with an expression or code to delete > multiple blank lines and replace it with only one blank line... > > I know how this can be done in sed, but am not sure how to do it in > perl... > > Thanks, > Taylor Well you could do this the simple way, use a switch that toggles on/off whether the last line you checked was empty. If the switch is on, and the current line is empty simply skip it. If the switch is off and the current line is ...

  1. #1

    Default Re: Delete multiple blank lines


    > Hi all, I could use some help with an expression or code to delete
    > multiple blank lines and replace it with only one blank line...
    >
    > I know how this can be done in sed, but am not sure how to do it in
    > perl...
    >
    > Thanks,
    > Taylor
    Well you could do this the simple way, use a switch that toggles on/off
    whether the last line you checked was empty. If the switch is on, and
    the current line is empty simply skip it. If the switch is off and the
    current line is empty then keep it, toggle the switch and move on.

    KISS,

    [url]http://danconia.org[/url]
    Wiggins D Anconia Guest

  2. #2

    Default Re: Delete multiple blank lines

    On Tue, Jan 20, 2004 at 12:39:55PM -0600, Lewick, Taylor wrote:
    > Hi all, I could use some help with an expression or code to delete
    > multiple blank lines and replace it with only one blank line...
    >
    > I know how this can be done in sed, but am not sure how to do it in
    > perl...
    perl -i~ -p00e0 file1 file2 ...

    perldoc perlrun

    --
    Paul Johnson - [email]paulpjcj.net[/email]
    [url]http://www.pjcj.net[/url]
    Paul Johnson Guest

  3. #3

    Default Re: Delete multiple blank lines

    On Jan 20, 2004, at 12:39 PM, Lewick, Taylor wrote:
    > Hi all, I could use some help with an expression or code to delete
    > multiple blank lines and replace it with only one blank line...
    >
    > I know how this can be done in sed, but am not sure how to do it in
    > perl...
    Well, if slurping is okay (read: file size is reasonable), I would
    probably use something like this (untested code):

    #!/usr/bin/perl

    use strict;
    use warnings;

    die "Usage: perl script_name OLD_FILE NEW_FILE\n" unless ARGV == 2;

    open OLD, '<', shift() or die "File error: $!\n";
    my $text = join '', <OLD>;
    close OLD;

    $text =~ s/\n(?:\s*\n)+/\n/g;

    open NEW, '<', shift() or die "File error: $!\n";
    print NEW $text;
    close NEW;

    __END__

    Hope that helps.

    James

    James Edward Gray II Guest

  4. #4

    Default Re: Delete multiple blank lines

    On Jan 20, 2004, at 1:01 PM, Paul Johnson wrote:
    > perl -i~ -p00e0 file1 file2 ...
    Wow! I've been over here trying to explain to myself why that works.
    Help me out a little.

    Broken down those switches are:

    -i~ -p -00e0

    Right?

    No problems with the first two here. The third sets the input record
    separator to 0e0, I think. My 'perlrun' said 00 was a special value
    used to get "paragraph mode". I'm going to assume 0e0 does the same
    thing.

    Here's where I'm fuzzy though, does "paragraph mode" only leave one \n
    at the end of $_? I didn't know that. Is it true even if you set if
    the usual way?

    local $/ = '';

    One last question, does "paragraph mode" consider lines with only
    spaces "blank lines" or must it be back-to-back newlines?

    Thanks for the lesson!

    James

    James Edward Gray II Guest

Similar Threads

  1. Extra Blank Lines in Code
    By algolvet in forum Macromedia Contribute General Discussion
    Replies: 0
    Last Post: October 14th, 09:37 PM
  2. How to remove blank lines?
    By fibble22 in forum Macromedia Contribute General Discussion
    Replies: 0
    Last Post: June 4th, 09:25 PM
  3. Blank Lines in program code
    By scweblady in forum Macromedia Contribute General Discussion
    Replies: 0
    Last Post: December 2nd, 01:36 AM
  4. datagrid blank lines
    By frankgrimes webforumsuser@macromedia.com in forum Macromedia Flash Actionscript
    Replies: 2
    Last Post: January 20th, 08:19 PM
  5. You're posting 250 or so blank lines each time
    By rifleman in forum Windows XP/2000/ME
    Replies: 2
    Last Post: July 11th, 12:44 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139