Rearranging columns using awk

awktext processing

I am trying to move 7th column of my csv file to the end by using

awk -F '{print $1,$2,$3,$4,$5,$6,$8,$9,$10,$11,$7}',OFS= "$file"

where $file is a .csv file in a directory. However, the output is

awk:                          ^ syntax error

Does anyone know how to fix this error?

Best Answer

The -F option needs an argument (field separator): -F, for example.

The end of the awk script must be separated with a (space char) with the rest of the parameters.

If the field separator is , and you wish to keep it, and if the number of column is constant and lower than or equal to 11, give a try to this:

awk -F, '{print $1,$2,$3,$4,$5,$6,$8,$9,$10,$11,$7}' OFS=, "$file"

If your field separator is a semicolon don't forget to set it in quotes like so

awk -f';' '{print $1,$2,$3,$4,$5,$6,$8,$9,$10,$11,$7}' OFS=';' "$file"
Related Question