awk - loop from a to z

Hello,

I was wondering if it is possible to do a loop on letters rather than numbers with awk (gawk).

Basically I used to do:

echo "nothing" | gawk '{for(i=1;i<11;i++)print i}'

But I would like to do something like that (which obviously does not work):

echo "nothing" | gawk '{for(i in a..z)print i}'

Any suggestion how to loop on the alphabet?

Thanks!

EDIT: At least I would need to be able to increment on the alphabet...

I don't think plain awk has anything to turn an int into a char, but how about:

echo | awk 'BEGIN {  split("ABCDEFGHIJKLMNOPQRSTUVWXYZ", A, ""); }
{ for(N=1; N<=26; N++) printf("A[%d]=%d\n", N, A[N]); }'

You can use the %c format specifier. The following prints the lowercase alphabet (one letter per line):

awk 'BEGIN { for (i=97; i<123; i++) printf("%c\n", i) }'

Regards,
Alister

1 Like
 echo {a..z} |xargs -n1
echo "nothing" | [g]awk '{gsub(""," ");for(i=1;i<=NF;i++) print $i}'

Thanks!

All the solutions proposed work fine.
For my use, Alister's is the most convenient (compact and self-contained).

for i in {a..z} ; do  echo $i; done