Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Break a file into seperate files based on string match

by karlgoethebier (Abbot)
on Dec 30, 2017 at 13:43 UTC ( [id://1206455]=note: print w/replies, xml ) Need Help??


in reply to Break a file into seperate files based on string match

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1206455]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-03-29 15:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found