Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

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



  • 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, 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, 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.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others contemplating the Monastery: (3)
    As of 2021-04-16 21:22 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?