hello,
a fast question:
how can i loop over four numbers in awk like in bash:
n ist a number >3
n=4
for i in 1 2 $((n-1)) $n
do .. done
i tried:
awk: for (i in 1 2 n-1 n)
with all possible brackets (){} and punctuations , ;
but this doesn't work...
alister
2
Put the values in an array and to iterate in order use a for loop with an index variable.
Regards,
Alister
1 Like
rdrtx1
3
try a loop like:
echo "" | awk '{x=4 ; while (i++ <x) print i}'
Subbeh
4
You could try somehting like this:
awk 'BEGIN{n=4; s="1,2,"n-1; split(s,a,","); for (i in a) print a}'
1 Like
EDIT : The solution below does not fit the question, sorry about that :X
Is this OK?
~/unix.com$ awk 'BEGIN{n=4; for(i=1; i<=n; i++)print i}'
1
2
3
4
~/unix.com$ awk 'BEGIN{n=4; while(i++<n)print i}'
1
2
3
4
RudiC
6
Maybe not quite what you need, but at least pointing in the desired direction:
awk '{for (i=n-2; i<=n+1; i++) print i%n+1} ' n=15 file
14
15
1
2
A little sort might help...
1 Like
@alister: works, but not very elegant
@tukuyomi, rdrtx1: n can be >>> 4
i need the first two (1,2) and the last two (n-1,n)...
@rudiC:
elegant, modulo! the order is no problem
@Subbeh: could work, I will try...
rdrtx1
8
try also:
awk '{for (i=1; i<=n; i++) if (i<=2 || i>n-2) print i} ' n=1000 infile