perl -F, -ane '`echo $_ >> $F[2].csv`' < file
The following command line options are used:
-n loop around each line of input file-l removes new lines before processing and adds them back-a automatic mode - splitting input lines into @F array. By default it is split into spaces.-e execute perl code-F auto-expansion modifier, in this case is broken into ,
@F is an array of words in each line, indexed starting at $F[0]
If you want to keep the title, you will need a more complex approach.
perl splitintofiles.pl file
Contents of splitintofiles.pl:
open $fh, '<', $ARGV[0]; while ($line = <$fh>) { print $line; if ($. == 1) { $header = $line; } else {
input:
a,b,c,d,e,f,g,h,i,j,k,l 19980102,,PLXS,10032,Q,A,,,15.12500,15.00000,15.12500,2 19980105,,PLXS,10032,Q,A,,,14.93750,14.75000,14.93750,2 19980106,,PLXS,10032,Q,A,,,14.56250,14.56250,14.87500,2 20111222,,PCP,63830,N,A,,,164.07001,164.09000,164.12000,1 20111223,,PCP,63830,N,A,,,164.53000,164.53000,164.55000,1 20111227,,PCP,63830,N,A,,,165.69000,165.61000,165.64000,1
PCP.csv output
a,b,c,d,e,f,g,h,i,j,k,l 20111222,,PCP,63830,N,A,,,164.07001,164.09000,164.12000,1 20111223,,PCP,63830,N,A,,,164.53000,164.53000,164.55000,1 20111227,,PCP,63830,N,A,,,165.69000,165.61000,165.64000,1
output of PLXS.csv
a,b,c,d,e,f,g,h,i,j,k,l 19980102,,PLXS,10032,Q,A,,,15.12500,15.00000,15.12500,2 19980105,,PLXS,10032,Q,A,,,14.93750,14.75000,14.93750,2 19980106,,PLXS,10032,Q,A,,,14.56250,14.56250,14.87500,2