Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2024-04-23 09:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found