Print one's place for 1 to N times, ksh Perl whatever?

Hello all,
I would like to create a for loop or whatever is quick that will print the one�s place of a number for 1-N times
say for example a printed page formatting is 132 characters wide,
I would like a single line

123456789012345678901234567890... ...012

That is 132 characters long. I am putting this in a simple print test program and want the recipient to see at a glance that the printing is not getting truncated for any reason.

I see in a similar thread a slick way to print a single character N times

In these below cases if one * was missing (due to printer/lp miss configuration) in a string of 132 your eye could miss it easily.. TIA. -KJ

nawk 'BEGIN{$1000=OFS="";print}' < /dev/null
perl -le 'print "
" x 1000'
printf "%01000d"|tr "0" "*"

$ maxv=80

$ numb=1; while [ $numb -le $maxv ]; do echo $numb | awk '{printf "%1s",substr($numb,length($numb),1)}'; numb=$(($numb+1)) ; done
12345678901234567890123456789012345678901234567890123456789012345678901234567890

$
1 Like

This is a breeze in Perl if you have Tie::Cycle installed. You tie a scalar to this class and provide the list of values to cycle through (1 to 9, 0). Then, whenever you access the scalar, the next element in the list is fetched. It becomes as easy as saying "print 'this' 132 times". 'this' cycles through that list and gets reset automatically.

Without this simple but wonderful module, you may try:

perl -le '$s="1234567890";print $s x (132/10) . substr($s,0,132%10)'

Just replace 132 with whatever number you need.

1 Like
perl -e 'print $_%10 for (1..132)'

where 132 is the length you are testing.

2 Likes

Sweet...

1 Like

Something similar using awk

awk 'BEGIN{while(++i<=132)printf i%10}'

--ahamed

1 Like

what os is this from? the while loop works perfectly, something in the awk line for me does not... I get this
awk: Field $() is not correct.
The input line number is 1.
The source line number is 1.
awk: Field $() is not correct.
The input line number is 1.
The source line number is 1.

N times. Thank you for your time. -just wondering if my awk has different options. -KJ

---------- Post updated at 04:40 PM ---------- Previous update was at 04:37 PM ----------

This fully works for me right now. You just made my day... in fact you all have thanks for all the Reponses. I now just need to look up how to pass the 132 as a var and i'm toast with this project. -KJ :smiley: I have not written perl since '98 lol

---------- Post updated at 05:05 PM ---------- Previous update was at 04:40 PM ----------

#cat temp
echo "Perl test"
export Width=80
perl -e 'print $_%10 for (1..'$Width')'
echo "\nAwk test"
export Width=132
echo $Width
awk 'BEGIN{while(++i<='$Width')printf i%10}'
#./temp
Perl test
12345678901234567890123456789012345678901234567890123456789012345678901234567890
Awk test
132
123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012
$ n=1;while (( p = n % 10 , n++ < 133 ));do echo -n $p;done;echo
123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012
$
awk 'BEGIN{while(++i<=x)printf i%10}' x=132

--ahamed

I tried that. Does not work, at least not in gawk, where x does not get initialized until awk starts reading lines.

You are right. Sorry about that. It will not get initialised in the BEGIN block.

--ahamed

No problema. I went through the same exercise, so I had already made exactly the same assumption.