Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In:</p> <pre><code>awk $line -v ... </code></pre> <p>$line holds the output of a grep, probably not something awk expects to see on it's command line. Also,m this:</p> <pre><code>for ((j=1; j&lt;=$number_of_columns; j++)) do anything &gt; "history_files/history${i}" done </code></pre> <p>will cause you to overwrite the history file every time through the loop. I don't know what you really wanted there.</p> <p>You have a slew of other issues with your script, though. You said "As an example number_of_columns = 3 and selected_columns = [2 5 8], so I want to print word number 2, 5 and 8 from the string line to the file history${i}.".</p> <p>That's trivial entirely in awk and you don't need to do a "grep" outside of awk either, so you could just do the whole thing as:</p> <pre><code>awk -v pat=" ${i}:\)" -v selected_columns="$selected_columns" ' BEGIN { number_of_columns = split(selected_columns,selected_columnsA) } $0 ~ pat { sep="" for (j=1;j&lt;=number_of_columns;j++) { current_column = selected_columnsA[j] printf "%s,%s",sep,lineA[current_column] sep = "\t" } print "" } ' "$1" &gt; "history_files/history${i}" </code></pre> <p>If that doesn't work for you, let's fix THAT instead of trying to fix the original script. Sounds like you have enclosing loop outside of the above, chances are that could just be part of the awk script as well.</p> <p>EDIT based on updated OP:</p> <p>I've added lots of comments but let me know if you have questions:</p> <pre><code>$ cat file 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 $ $ cat tst.sh selected_columns=(2 5 8) selCols="${selected_columns[@]}" awk -v selCols="$selCols" ' BEGIN { # Executed before the first line of the input file is read # Split the string of selected column numbers, selCols, into # an array selColsA where selColsA[1] has the value of the # first space-separated sub-string of selCols (i.e. the number # of the first column to print). Note that we dont need the # number of columns passed into the script as a result of # splitting the string is the count of elements put into the # array as a return code from the split() builtin function. numCols = split(selCols,selColsA) } { # Executed once for every line of the input file # Create a numerix suffix like "45" from the first column # in the current line of the input file, e.g. "45:)" by # just getting rid of all non-digit characters. sfx = $1 gsub(/[^[:digit:]]/,"",sfx) # Create the name of the output file by attaching that # numeric suffix to the base value for all output files. #histfile = "history_files/history" sfx histfile = "tmp" sfx # Loop through every column we want printed. selColsA[&lt;index&gt;] # gives us a column number which we can then use to access the # columns of the current line. Awk uses the builtin variable $0 # to hold the current line, and it autolatically splits it so # that $1 holds the first column, $2 is the second, etc. So # if selColsA[1] has the value 3, then $(selColsA[1]) would be # the value of the 3rd column of the current input line. sep="" for (i=1;i&lt;=numCols;i++) { curCol = selColsA[i] # Print the current column, prefixed by a tab for all but # the first column, and without a terminating newline so the # next column gets appended to the end of the current output line. # Note that in awk "&gt; file" has different semantics from shell # and opens the file for writing the first time the line is hit # like "&gt; file" in shell, but then appends to it every time its # hit afterwards, like "&gt;&gt; file" in shell. printf "%s%s",sep,$curCol &gt; histfile sep = "\t" } # Add a newline to the end of the current output line print "" &gt; histfile } ' "$1" $ $ ./tst.sh file $ $ cat tmp1 3.593E-02 2.780E+02 1.000E+05 $ cat tmp2 3.593E-02 2.780E+02 1.000E+05 $ cat tmp44 2.884E-02 2.780E+02 9.990E+02 $ cat tmp45 2.884E-02 2.780E+02 9.990E+02 </code></pre> <p>By the way, I used the words "column" and "line" above for your benefit since you're just learning, but FYI the awk terminology is actually "field" and "record".</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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