title: Use Perl to extract fields from a large text file using column numbers from another file
body: <p>I have two files</p>
  
  <ul>
  <li><p>A text file with a list of numbers, e.g.</p>
  
  <pre><code>1
  2
  3
  5
  7
  6
  </code></pre>
  
  <p>each number is on a different line, and the row number corresponds to a column number in the second file.</p></li>
  <li><p>A large text file with ~150,000 columns. I need to extract only the columns listed in the first file.</p></li>
  </ul>
  
  <p>I would prefer to do this command-line-style so I came up with this</p>
  
  <pre><code>for i in columns.txt do 
  
  perl -lane 'print $F[i]' final.txt &gt; output.txt
  </code></pre>
  
  <p>I want to do something like this, but without combining Bash and script Perl</p>
  
  <pre><code>for ($i = 0; $i &lt; $num_col; $i++) {          # for each line in the column file 1
      my $column = $columns[$i];               # extract the number from the line
      perl -lane 'print $F[$column]' final.txt &gt; output.txt; # and cut that column from file #2 into output file
  }
  </code></pre>
  
  <p>How to do this without combining a Perl script and Bash? </p>
  
  <p>I am new to scripting so explanations would be great as well as code help.</p>
  
  <p>final looks like this but with dimensions 106713 x 119,962</p>
  
  <p>6665 AA AG TG CC GG GT TA TT</p>
  
  <p>6667 AT TC AT CG GA GA TC AA</p>
  
  <p>6668 AC TC TT CA GT GA TC CG</p>
  
  <p>6669 AG AC AA CT GG GA TC CA</p>
  
  <p>6670 AA AT AG AC GG GA TC AA</p>
  
  <p>ID 2 2 1 1 1 6 6 1                                        #this single row is the columns.txt file</p>
  
  <p>ID rs3755048 rs2276637 rs1043502 rs879089 rs647812 rs2076310 c6_pos32913147 rs1051741</p>
  
  <p>ID 0 0 0 0 0 0 0 0</p>
  
  <p>ID 219793146 219797929 20850335 20841103 20866804 33274012 32913147 224098852</p>