Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

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

Hello coolda,

Here is one approach, using Tie::File to make it easier to repeatedly append to each line of the output file:

“file1.dat”:

Gene exp1 exp2 exp3 exp4 1 a b c d 2 e f g h 3 i j k l 4 m n o p 5 q r s t 6 u v w x

“file2.dat”:

Gene exp1 exp2 exp3 exp4 1 aa bb cc dd 2 ee ff gg hh 3 ii jj kk ll 4 mm nn oo pp 5 qq rr ss tt 6 uu vv ww xx

Script:

#! perl use strict; use warnings; use Tie::File; use constant { FIELD => 3, # Number of the field to extract from each record SEP => ',', # Separator for fields in each output file record }; my @infile_names = qw( file1.dat file2.dat ); my $outfile_name = 'exp3.dat'; open(my $in, '<', $infile_names[0]) or die "Cannot open file '$infile_names[0]' for reading: $!"; open(my $out, '>', $outfile_name) or die "Cannot open file '$outfile_name' for writing: $!"; <$in>; # Discard header print $out $infile_names[0], "\n"; while (my $line = <$in>) { my @fields = split /\s+/, $line; print $out $fields[FIELD], "\n"; } close $out or die "Cannot close file '$outfile_name': $!"; close $in or die "Cannot close file '$infile_names[0]': $!"; tie my @array, 'Tie::File', $outfile_name or die "Cannot tie file '$outfile_name': $!"; for my $file (@infile_names[1 .. $#infile_names]) { open(my $in, '<', $file) or die "Cannot open file '$file' for reading: $!"; <$in>; # Discard header $array[0] .= SEP . $file; while (my $line = <$in>) { my @fields = split /\s+/, $line; $array[$. - 1] .= SEP . $fields[FIELD]; } close $in or die "Cannot close file '$file': $!"; } untie @array;

Output in “exp3.dat”:

file1.dat,file2.dat c,cc g,gg k,kk o,oo s,ss w,ww

Another approach you should consider is to store the hundreds-of-files’ worth of data in a database, and then extract whatever you need via SQL.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re: Is there any efficient way i can take out a specific column from hundreds of files and put it in one file? by Athanasius
in thread Is there any efficient way i can take out a specific column from hundreds of files and put it in one file? by coolda

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 having an uproarious good time at the Monastery: (4)
As of 2024-03-28 22:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found