Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Split tab-separated file into separate files, based on column name

by Tux (Canon)
on Aug 26, 2020 at 12:33 UTC ( [id://11121097]=note: print w/replies, xml ) Need Help??


in reply to Split tab-separated file into separate files, based on column name

OK, I'll bite. A one-liner it is:

$ cat test.tsv id name position 1 Nick boss 2 George CEO 3 Christina CTO $ perl -MText::CSV_XS=csv -E'my$aoh=csv(in=>"test.tsv",bom=>1,sep=>"\t +");' \ -E'for$h(keys%{$aoh->[0]}){say$h;open$fh,">","$h.txt";say$fh $_ + for$h,map{$_->{$h}}@$aoh}' id position name $ cat id.txt id 1 2 3 $ cat name.txt name Nick George Christina $ position boss CEO CTO

update: added a -E to split the line for readability


Enjoy, Have FUN! H.Merijn
  • Comment on Re: Split tab-separated file into separate files, based on column name
  • Download Code

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2024-04-18 20:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found