I split a tab-delimited file into multiple files based on the value in column one. I modified another person's code and can't figure out how to give the output files custom names. Now I'm trying to rename the files (which all have different names) based on the contents of a tab-delimited text file (again all of the new names are unique).

I know the code to do a single rename but I have been unable to get the correct syntax to rename all files based on the contents of the txt document. I have 736 output files and will likely have to do this in the future with different files, so while I could rename one at a time, it seems like I'm better off in the longrun if I can rename all of the output files using the txt file.

I could also fix the problem with the original split script if I could customize the output names (I could get rid of the intermediate rename script all together). In the program I can modify the output names, but if I change one part of the name (%03d), then I get errors about the file not being clobbered.

Here is the split script.

# config: my $field = 0; my $sep = "\t"; $, = $sep; $\ = $/; my %file; # { ID, sequence, $fh } my $fID = 1; while (<INFILE>) { chomp; my @c = split /$sep/o; my( $key, $ID ) = defined $c[$field] ? ( $c[$field], $fID++ ) : ( '(column not present)', 0 ); unless ( $file{$key} ) { $file{$key}{ID} = $ID; $file{$key}{sequence} = sprintf '%03d.tabular', $file{$key}{ID +}; -f $file{$key}{sequence} and die "Sorry, '$file{$key}{ID}' exists; won't clobber."; open $file{$key}{fh}, ">", $file{$key}{sequence} or die "Error opening '$file{$key}{sequence}' for write - $!"; } print {$file{$key}{fh}} @c; } print OUTFILE $file{$_}{sequence}, $_ for sort { $file{$a}{ID} <=> $file{$b}{ID} } keys %file;

In reply to Renaming Multiple Files with Different Names by Feral_Akodon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.