I have the following awk command which works perfectly:
awk 'BEGIN { ORS = " " } {print 22, $1, NR; for(i=2;i<=NF;++i) print $i}{print "\n"}' file
What it does is insert two columns: the value 22 as the first, and the nr of the row as the third (see this question).
I tried running this command in a for loop as follows:
for p in 20 21 22
do
awk 'BEGIN { ORS = " " } {print $p, \$1, NR; for(i=2;i<=NF;++i) print \$i}{print "\n"}' file > file$p
done
So instead of the 22 in the first column, 3 files are made where each file has 20, 21 or 22 as the first column. Unfortunately, this doesn’t work and I get the message: ^ backslash not last character on line.
It probably has something to do with how I escaped the $ characters, but I don’t know how else to do it… any ideas?
You can assign a shell-parameter to awk using
i.e. use
Like this:
Complete command: