This example will use the last directory as the label, so it is not limited to using just BAKERY or FRUITJUNCTION and it won't break if those patterns occur elsewhere in the path (see the last two example paths).

use strict; use warnings; # for portable path and filename operations use File::Spec; my @paths = qw( /home/units/bakery/cake.pl /home/units/bakery/pie.pl /home/units/bakery/plumcake.pl /home/units/fruitjunction/juice.pl /home/units/fruitjunction/icecream.pl /home/units/bakery/choclate.pl /home/bakery/fruitjunction/not_a_bakery.pl /home/units/foo/bar.pl ); my $current_dir = ''; foreach my $path ( @paths ) { chomp $path; # retrieve the last directory in the path my $dir = get_dir( $path ); # print a start label if this is the first path if( $current_dir eq '' ) { $current_dir = uc( $dir ); print "# $current_dir\n"; } # change labels if required if( uc( $dir ) ne $current_dir ) { print "# END $current_dir\n"; $current_dir = uc( $dir ); print "# $current_dir\n"; } print "$path\n"; } print "# END $current_dir\n"; sub get_dir { my ( $path ) = @_; # extract the directory portion of the path my ( undef, $dirs, undef ) = File::Spec->splitpath( $path ); # remove trailing directory separators $dirs = File::Spec->canonpath( $dirs ); # split the path on directory separators my @dir = File::Spec->splitdir( $dirs ); return $dir[-1]; }
Output:
# BAKERY /home/units/bakery/cake.pl /home/units/bakery/pie.pl /home/units/bakery/plumcake.pl # END BAKERY # FRUITJUNCTION /home/units/fruitjunction/juice.pl /home/units/fruitjunction/icecream.pl # END FRUITJUNCTION # BAKERY /home/units/bakery/choclate.pl # END BAKERY # FRUITJUNCTION /home/bakery/fruitjunction/not_a_bakery.pl # END FRUITJUNCTION # FOO /home/units/foo/bar.pl # END FOO


In reply to Re: insert lines based on some pattern by bobf
in thread insert lines based on some pattern by Anonymous Monk

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.