you don't need awk for that — either seq or jot alone suffices :
% seq -f '%05.f' 6 # bsd-seq 00001 00002 00003 00004 00005 00006 % gseq -f '%05.f' 6 # gnu-seq 00001 00002 00003 00004 00005 00006 % jot -w '%05.f' 6 00001 00002 00003 00004 00005 00006
…… unless you're going into bigint territory :
% gawk -Mbe ' function __(_,___) { return +_<+___?___:_ } BEGIN { _+=_^=_<_ ____="%0*.f\n" } { ___=__($--_, !+$++_) _____=__(++_+--_, length(______=+$NF)) do { printf(____,_____,___) } while (___++<______) }' <<< '999999999999999999996 1000000000000000000003' 0999999999999999999996 0999999999999999999997 0999999999999999999998 0999999999999999999999 1000000000000000000000 1000000000000000000001 1000000000000000000002 1000000000000000000003
——————————————————————————————————————————————————
If you need to print out a HUGE range of numbers, then this approach maybe a bit faster -
printing out every integer from 1 to 1 million, left-zero-padded to 9-digits wide, in 0.049s
*caveat : I didn't have the spare time to make it cover all input ranges :: it's just a proof of concept accepting increments of powers of 10
——————————————————————————————————————————————————
( time ( LC_ALL=C mawk2 ' function jot(____,_______,_____,_,__,___,______) { if(____==(____^!____)) { return +____<+_______\ ? sprintf("%0*.f",_______,____)\ : +____ } _______= (_______-=____=length(____)-\ (_=!(_<_)))<+_ \ ? "" \ : sprintf("%0*.f",_______,!_) __=_= (!(__=_+=_+_))(__=(-+--_)+(__+=_)^++_)\ (__+=_=(((_--^_--+_++)^++_-_^!_)/_))(__+_) _____= "." gsub(_____,"\\&&",__) ____—- do { gsub(_____,__,_) _____=_____"." } while(—____) gsub(_____,(_______)"&\n",_) sub("^[^\n]+[\n]","",_) sub(".$",""~"",_______) return \ (_)(_______)\ sprintf("%0*.f",length(_____),__<__) } { print jot($1,$2) }' <<< '10000000 9' ) | pvE9 ) |xxh128sum |ggXy3 | lgp3 sleep 2 ( time ( LC_ALL=C jot 1000000 | LC_ALL=C mawk2 '{ printf("%09.f\n", $1) }' ) | pvE9 ) |xxh128sum |ggXy3 | lgp3 out9: 9.54MiB 0:00:00 [ 275MiB/s] [ 275MiB/s] [<=> ] ( LC_ALL=C mawk2 <<< '1000000 9'; ) 0.04s user 0.01s system 93% cpu 0.049 total e0491043bdb4c8bc16769072f3b71f98 stdin out9: 9.54MiB 0:00:00 [36.5MiB/s] [36.5MiB/s] [ <=> ] ( LC_ALL=C jot 1000000 | LC_ALL=C mawk2 '{printf("%09.f\n", $1)}'; ) 0.43s user 0.01s system 158% cpu 0.275 total e0491043bdb4c8bc16769072f3b71f98 stdin
By the time you're doing 10 million, the time differences become noticeable :
out9: 95.4MiB 0:00:00 [ 216MiB/s] [ 216MiB/s] [<=> ] ( LC_ALL=C mawk2 <<< '10000000 9'; ) 0.38s user 0.06s system 95% cpu 0.458 total be3ed6c8e9ee947e5ba4ce51af753663 stdin out9: 95.4MiB 0:00:02 [36.3MiB/s] [36.3MiB/s] [ <=> ] ( LC_ALL=C jot 10000000 | LC_ALL=C mawk2 '{printf("%09.f\n", $1)}'; ) 4.30s user 0.04s system 164% cpu 2.638 total be3ed6c8e9ee947e5ba4ce51af753663 stdin out9: 95.4MiB 0:00:02 [35.2MiB/s] [35.2MiB/s] [ <=> ] ( LC_ALL=C python3 -c '__=1; ___=10**7; [ print("{0:09d}".format(_)) for _ in range(__,___+__) ]' ) | pvE9 ) | xxh128sum |ggXy3 | lgp3 ; ) 2.68s user 0.04s system 99% cpu 2.725 total be3ed6c8e9ee947e5ba4ce51af753663 stdin
for variable in $(something to generate the numbers); do ...but this is problematic when the list of numbers is long. It is much more efficient to usesomething to generate the numbers | while read -r variable; do .... See also mywiki.wooledge.org/DontReadLinesWithFor which discusses reading lines from files etc, but some of the arguments apply here too.