Shell awk loop and variable substitution

Hello again,

I'm trying to loop through columns where the last column is a stored value. For example:

Code:
$ echo $columns
1a 2b 3c 4d 5e 6f 7g 8h 9i 10j 11k 12l 13m 14n 15o 16p
$ echo $mycolumn
12
$ echo $columns | awk '{for(i=8;i<=12;++i)print $i}'
8h
9i
10j
11k
12l

So, I want to use $mycolumn:

Code:
echo $columns | awk -v n=$mycolumn '{print $n}'
12l

However, when I try:

$ echo $columns | awk -v n=$mycolumn '{for(i=8;i<=$n;++i)print $i}'

I get no output.

Would you please help?
 
As juha said, using sh for the shell:
Code:
# columns="1a 2b 3c 4d 5e 6f 7g 8h 9i 10j 11k 12l 13m 14n 15o 16p"
# mycolumn="12"
# echo $columns | awk -v n=$mycolumn '{for(i=8;i<=n;i++)print $i}'
# echo $columns | awk '{for(i=8;i<='$mycolumn';i++)print $i}'
(I hope this isn't bad form, reproducing in code what somebody else said in words?)
 
Back
Top