A solution using Path::Tiny, map and grep:

#!/usr/bin/env perl use strict; use warnings; use Path::Tiny; use Data::Dump; use feature qw(say); my @data = path("data.txt")->lines( { chomp => 1 } ); dd \@data; my @tag1 = map { "$_\n" } grep { defined } map { /(.+) : tag1/; $1 } @data; my @tag2 = map { "$_\n" } grep { defined } map { /(.+) : tag2/; $1 } @data; dd \@tag1; dd \@tag2; path("tag1.txt")->spew(@tag1); path("tag2.txt")->spew(@tag2); say path("tag1.txt")->slurp; say path("tag2.txt")->slurp; __END__ karls-mac-mini:hvirani karl$ ./hvirani.pl [ "test/foo/bar : tag1", "test/abc/xyz : tag1", "test/def/abc : tag2", "test/bar/foo : tag2", "test/dummy/foo : tag1", ] ["test/foo/bar\n", "test/abc/xyz\n", "test/dummy/foo\n"] ["test/def/abc\n", "test/bar/foo\n"] test/foo/bar test/abc/xyz test/dummy/foo test/def/abc test/bar/foo

Best regards, Karl

Minor update: Added missing square bracket in output.

«The Crux of the Biscuit is the Apostrophe»

perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help


In reply to Re: Break a file into seperate files based on string match by karlgoethebier
in thread Break a file into seperate files based on string match by hvirani

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.