Note that there are some explanatory texts on larger screens.

plurals
  1. POPrinting column from a string in bash
    text
    copied!<p><strong>UPDATED QUESTION</strong> Ok, so I have a file with lines like this:</p> <pre><code>44:) 2.884E-02 0.000E+00 0.000E+00 2.780E+02 0.000E+00 0.000E+00 9.990E+02 45:) 2.884E-02 0.000E+00 0.000E+00 2.780E+02 0.000E+00 0.000E+00 9.990E+02 1:) 3.593E-02 0.000E+00 0.000E+00 2.780E+02 0.000E+00 0.000E+00 1.000E+05 2:) 3.593E-02 0.000E+00 0.000E+00 2.780E+02 0.000E+00 0.000E+00 1.000E+05 </code></pre> <p>The numbers in the first column run from 1 to x (in this case 45) and then starts over at 1 lots of times. I want to move some of the columns to a separate file. The indexes of the columns I want to move is stored in the variable/array <code>$selected_columns</code> (in this case 2, 5 and 8) and the number of columns I want to move is stored in <code>$number_of_columns</code> (in this case 3).</p> <p>I then want to create 45 files, one for the selected columns for all <code>1:)</code>, one for the selected columns for all <code>2:)</code> and so forth. I want to make this as general as possible since both the number of columns and the number running from 1 to x will change. The number x is always known and the columns to extract are chosen by the user.</p> <p><strong>ORIGINAL QUESTION:</strong></p> <p>I have a string fetched by egrep. Then I want to print some of the columns (words) in that string. The position (column index) is known in a list in my bash script. Currently it looks like this:</p> <pre><code>line=$(egrep " ${i}:\)" $1) for ((j=1; j&lt;=$number_of_columns; j++)) do awk $line -v current_column=${selected_columns[$j]} '{printf $(current_column)}' &gt; "history_files/history${i}" done </code></pre> <p>where <code>number_of_columns</code> is the number of columns that are to be printed and <code>selected_columns</code> contain the corresponding indexes of those columns. As an example <code>number_of_columns = 3</code> and <code>selected_columns = [2 5 8]</code>, so I want to print word number 2, 5 and 8 from the string <code>line</code> to the file <code>history${i}</code>.</p> <p>I am not sure what is wrong, but this has been done with some trial and error. The current error is <code>awk: cannot open 0.000E+00 (No such file or directory)</code>.</p> <p>Any help is appreciated!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload