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