FAQ
I have a txt file of products that I would like to be able to insert a blank
line whenever the name of the product changes, to make it easier to read. I
found this srcipt hunting about but I don't know how to apply it to the text
file. I've tried all kinds of different things but just don't get it.



example of text file named: products.txt

LAWN SEED 34.00 RED
LAWN SEED1 36.00 BLUE
LAWN SEED1 36.00 BLUE
LAWN SEED1 36.00 BLUE
LAWN SEED2 37.00 PINK

#!/usr/bin/perl -w

my $file = shift @ARGV;

open IN, $file or die "Couldn't open $file: $!";
my $before_line;
while (my $new_line = <IN>) {
print "\n" if ( $before_line and $before_line ne $new_line );
print $new_line;
$before_line = $new_line;
}

close IN;

Search Discussions

  • Telemachus at Feb 15, 2009 at 2:07 pm

    On Sun Feb 15 2009 @ 7:25, Jack Butchie wrote:
    I have a txt file of products that I would like to be able to insert a
    blank line whenever the name of the product changes, to make it easier to
    read. I found this srcipt hunting about but I don't know how to apply it
    to the text file. I've tried all kinds of different things but just don't
    get it.
    Do you already have Perl installed on your computer? If so, you can run it
    as follows. Save the Perl script with whatever name you like - say,
    new_liner. Then enter this into a terminal:

    perl new_liner filename

    Instead of 'filename', enter the name of the file you want to work on. The
    two files should be in the same directory for this to work. Otherwise, you
    will need to enter the full path of the file,

    perl new_liner /path/to/filename

    You should get output like this:

    telemachus ~ $ perl new_liner feed
    LAWN SEED 34.00 RED

    LAWN SEED1 36.00 BLUE
    LAWN SEED1 36.00 BLUE
    LAWN SEED1 36.00 BLUE

    LAWN SEED2 37.00 PINK

    Assuming that the output comes out as you want it, you can save it to a new
    file with redirection:

    perl new_liner feed_file > new_feed_file

    In a nutshell, the script is taking a single filename as an argument,
    opening the file and then checking line n for equality with n-1. It will
    work with exactly the setup you describe, but it's not a very maintainable
    situation (a single extra space or typo will bork the equality test, there's
    no way to deal with too many or two few filenames entered on the command
    line, etc.).

    As a more general rule, I would have to recommend *not* running scripts you
    don't understand that you find floating around on the interwebs.

    Hope this helps, T
  • Jack Butchie at Feb 15, 2009 at 2:39 pm
    Wow, fast response. Thanks everyone.

    I keep getting a space where there shouldn't be. I am running this on our
    test server with windows 2000. Is Mr. Windows being bad?

    FENCES WHITE BAD
    FENCES WHITE BAD
    FENCES WHITE BAD
    FENCES WHITE BAD

    LAWN SEED GOOD BAD
    LAWN SEED GOOD BAD

    LAWN SEED GOOD BAD

    TREES BAD BAD
    TREES BAD BAD
    TREES BAD BAD
    TREES BAD BAD

    WAFFLES BAD BAD GOOD
    WAFFLES BAD BAD GOOD
    WAFFLES BAD BAD GOOD
    WAFFLES BAD BAD GOOD
    WAFFLES BAD BAD GOOD
    WAFFLES BAD BAD GOOD
    WAFFLES BAD BAD GOOD
    WAFFLES BAD BAD GOOD
    WAFFLES BAD BAD GOOD
    WAFFLES BAD BAD GOOD
    WAFFLES BAD BAD GOOD
    WAFFLES BAD BAD GOOD
    WAFFLES BAD BAD GOOD
    WAFFLES BAD BAD GOOD
    WAFFLES BAD BAD GOOD
    WAFFLES BAD BAD GOOD
    WAFFLES BAD BAD GOOD
    WAFFLES BAD BAD GOOD




    ----- Original Message -----
    From: "Telemachus" <telemachus@arpinum.org>
    To: <beginners@perl.org>
    Sent: Sunday, February 15, 2009 8:07 AM
    Subject: Re: Add Line break when first of a line changes

    On Sun Feb 15 2009 @ 7:25, Jack Butchie wrote:
    I have a txt file of products that I would like to be able to insert a
    blank line whenever the name of the product changes, to make it easier to
    read. I found this srcipt hunting about but I don't know how to apply it
    to the text file. I've tried all kinds of different things but just don't
    get it.
    Do you already have Perl installed on your computer? If so, you can run it
    as follows. Save the Perl script with whatever name you like - say,
    new_liner. Then enter this into a terminal:

    perl new_liner filename

    Instead of 'filename', enter the name of the file you want to work on. The
    two files should be in the same directory for this to work. Otherwise, you
    will need to enter the full path of the file,

    perl new_liner /path/to/filename

    You should get output like this:

    telemachus ~ $ perl new_liner feed
    LAWN SEED 34.00 RED

    LAWN SEED1 36.00 BLUE
    LAWN SEED1 36.00 BLUE
    LAWN SEED1 36.00 BLUE

    LAWN SEED2 37.00 PINK

    Assuming that the output comes out as you want it, you can save it to a
    new
    file with redirection:

    perl new_liner feed_file > new_feed_file

    In a nutshell, the script is taking a single filename as an argument,
    opening the file and then checking line n for equality with n-1. It will
    work with exactly the setup you describe, but it's not a very maintainable
    situation (a single extra space or typo will bork the equality test,
    there's
    no way to deal with too many or two few filenames entered on the command
    line, etc.).

    As a more general rule, I would have to recommend *not* running scripts
    you
    don't understand that you find floating around on the interwebs.

    Hope this helps, T


    --
    To unsubscribe, e-mail: beginners-unsubscribe@perl.org
    For additional commands, e-mail: beginners-help@perl.org
    http://learn.perl.org/
  • Gunnar Hjalmarsson at Feb 15, 2009 at 2:57 pm

    Telemachus wrote:
    On Sun Feb 15 2009 @ 7:25, Jack Butchie wrote:
    I have a txt file of products that I would like to be able to insert a
    blank line whenever the name of the product changes, to make it easier to
    read. I found this srcipt hunting about but I don't know how to apply it
    to the text file.
    <snip>
    In a nutshell, the script is taking a single filename as an argument,
    opening the file and then checking line n for equality with n-1.
    Hence it does not comply with the OP's requirement, i.e. a blank line
    whenever the *product name* changes.
    As a more general rule, I would have to recommend *not* running scripts you
    don't understand that you find floating around on the interwebs.
    Indeed.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
  • Jack Butchie at Feb 15, 2009 at 2:56 pm
    I did some fiddling with the list. I noticed if the second or third or
    whatever column wasn't exactly the same as the previous, even though the tex
    in the first field was the same, line was being added. I then tested with
    only the first field in the list and it worked perfectly. The script seems
    not to like anything different in other columns/fields.

    LAWN
    LAWN
    LAWN
    PORCHES
    WARTHOGS
    Wow, fast response. Thanks everyone.

    I keep getting a space where there shouldn't be. I am running this on our
    test server with windows 2000. Is Mr. Windows being bad?

    FENCES WHITE BAD
    FENCES WHITE BAD
    FENCES WHITE BAD
    FENCES WHITE BAD

    LAWN SEED GOOD BAD
    LAWN SEED GOOD BAD

    LAWN SEED GOOD BAD

    TREES BAD BAD
    TREES BAD BAD
    TREES BAD BAD
    TREES BAD BAD

    WAFFLES BAD BAD GOOD
    WAFFLES BAD BAD GOOD
    WAFFLES BAD BAD GOOD
    WAFFLES BAD BAD GOOD
    WAFFLES BAD BAD GOOD
    WAFFLES BAD BAD GOOD
    WAFFLES BAD BAD GOOD
    WAFFLES BAD BAD GOOD
    WAFFLES BAD BAD GOOD
    WAFFLES BAD BAD GOOD
    WAFFLES BAD BAD GOOD
    WAFFLES BAD BAD GOOD
    WAFFLES BAD BAD GOOD
    WAFFLES BAD BAD GOOD
    WAFFLES BAD BAD GOOD
    WAFFLES BAD BAD GOOD
    WAFFLES BAD BAD GOOD
    WAFFLES BAD BAD GOOD




    ----- Original Message -----
    From: "Telemachus" <telemachus@arpinum.org>
    To: <beginners@perl.org>
    Sent: Sunday, February 15, 2009 8:07 AM
    Subject: Re: Add Line break when first of a line changes

    On Sun Feb 15 2009 @ 7:25, Jack Butchie wrote:
    I have a txt file of products that I would like to be able to insert a
    blank line whenever the name of the product changes, to make it easier
    to
    read. I found this srcipt hunting about but I don't know how to apply
    it
    to the text file. I've tried all kinds of different things but just
    don't
    get it.
    Do you already have Perl installed on your computer? If so, you can run
    it
    as follows. Save the Perl script with whatever name you like - say,
    new_liner. Then enter this into a terminal:

    perl new_liner filename

    Instead of 'filename', enter the name of the file you want to work on.
    The
    two files should be in the same directory for this to work. Otherwise,
    you
    will need to enter the full path of the file,

    perl new_liner /path/to/filename

    You should get output like this:

    telemachus ~ $ perl new_liner feed
    LAWN SEED 34.00 RED

    LAWN SEED1 36.00 BLUE
    LAWN SEED1 36.00 BLUE
    LAWN SEED1 36.00 BLUE

    LAWN SEED2 37.00 PINK

    Assuming that the output comes out as you want it, you can save it to a
    new
    file with redirection:

    perl new_liner feed_file > new_feed_file

    In a nutshell, the script is taking a single filename as an argument,
    opening the file and then checking line n for equality with n-1. It will
    work with exactly the setup you describe, but it's not a very
    maintainable
    situation (a single extra space or typo will bork the equality test,
    there's
    no way to deal with too many or two few filenames entered on the command
    line, etc.).

    As a more general rule, I would have to recommend *not* running scripts
    you
    don't understand that you find floating around on the interwebs.

    Hope this helps, T


    --
    To unsubscribe, e-mail: beginners-unsubscribe@perl.org
    For additional commands, e-mail: beginners-help@perl.org
    http://learn.perl.org/
  • Telemachus at Feb 15, 2009 at 3:13 pm

    On Sun Feb 15 2009 @ 8:56, Jack Butchie wrote:
    I did some fiddling with the list. I noticed if the second or third or
    whatever column wasn't exactly the same as the previous, even though the
    tex in the first field was the same, line was being added. I then tested
    with only the first field in the list and it worked perfectly. The script
    seems not to like anything different in other columns/fields.

    LAWN
    LAWN
    LAWN
    PORCHES
    WARTHOGS
    Right, as I said (and Gunnar made clearer), this is *not* testing product
    names for equality. It's testing the entire lines for equality.

    Which field do you actually want to test? Now that I look at your initial
    email, I'm guessing that what you actually want to test is "LAWN SEED"
    against "LAWN SEED1" - which makes things a bit more complicated. Your file
    doesn't have a consistent field separator (sometimes a space is part of a
    product name, sometimes not). A solution that works for "LAWN SEED" and
    "LAWN SEED1" won't be identical to one for "LAWN SEED1" compared to
    "MULCH".

    So, what does the file really look like?
  • Gunnar Hjalmarsson at Feb 15, 2009 at 3:14 pm

    Jack Butchie wrote:
    I did some fiddling with the list. I noticed if the second or third or
    whatever column wasn't exactly the same as the previous, even though the
    tex in the first field was the same, line was being added.
    What's the field separator?

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
  • Jack Butchie at Feb 15, 2009 at 3:48 pm
    I used a tab, then a pipe, both produced the same results.

    LAWNS|123|GOOD

    LAWNS|12|GOOD

    ----- Original Message -----
    From: "Gunnar Hjalmarsson" <noreply@gunnar.cc>
    To: <beginners@perl.org>
    Sent: Sunday, February 15, 2009 9:16 AM
    Subject: Re: Add Line break when first of a line changes

    Jack Butchie wrote:
    I did some fiddling with the list. I noticed if the second or third or
    whatever column wasn't exactly the same as the previous, even though the
    tex in the first field was the same, line was being added.
    What's the field separator?

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl

    --
    To unsubscribe, e-mail: beginners-unsubscribe@perl.org
    For additional commands, e-mail: beginners-help@perl.org
    http://learn.perl.org/
  • Telemachus at Feb 15, 2009 at 4:24 pm

    On Sun Feb 15 2009 @ 9:48, Jack Butchie wrote:
    I used a tab, then a pipe, both produced the same results.

    LAWNS|123|GOOD

    LAWNS|12|GOOD
    The results are the same because the test is for the whole line. If you
    only want to test one field, you need a different script. For example, this
    will test the first field (only) of each line:

    #!/usr/bin/perl -w

    my $file = shift @ARGV;

    open IN, $file or die "Couldn't open $file: $!";
    my $before;

    while (my $line = <IN>) {
    my $now = (split /\|/, $line)[0];
    if ($before and $before ne $now) {
    print "\n";
    }
    print $line;
    $before = $now;
    }

    close IN;

    On the other hand, this is getting more and more hacked together, as you
    can see. The big problem remains that you are going to have a script that
    you can't maintain because you don't know how it works.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedFeb 15, '09 at 1:25p
activeFeb 15, '09 at 4:24p
posts9
users3
websiteperl.org

People

Translate

site design / logo © 2021 Grokbase