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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |