Jump to content

script to output every number in a sequence between x and y to a text file


Recommended Posts

Posted

Im trying to output every possible number between x and y to a text file, one on each line.

 

They all start 04xxxxxxxx so I want 0400000000 -> 0499999999

 

I tried in excel and got as far as 0400350000 by dragging down but figured there must be a much faster way to do it?

 

So I tried in Bash and came up with this:

 

#!/bin/bash
for i in `seq 0400000000  0499999999`;
       do
               echo $i
       done

 

which seems to work but it misses off the first 0. Is there any way to add it in as its running, or to the resultant file after?

Posted
@echo off

FOR /L %%A IN (400000000,1,499999999) DO ECHO 0%%A

 

Under cmd, just do:

 

blah.bat > test1.txt ?

 

 

 

Should work.

 

Steve

  • Thanks 1
Posted
dos 1 bash 0

 

works great

 

thanks :)

 

Will probably take a while unless you got an epic machine to run it on :) Seeing it's huge numbers, but should do what you want in the end.

 

Steve

Posted
is that how many there are, wow. Might take longer than I thought.

 

They are the mobile phone numbers for this country.

 

Ah, Unless I miscounted:

 

0400,000,000 -> 0499,999,999

 

400million to 499 million :)

 

That's a lot of phones! :) Spam a lot? lol

 

Steve

Posted (edited)
Will probably take a while unless you got an epic machine to run it on

Only took me 3 minutes 56 seconds. :)

 

$stream = New-Object System.IO.StreamWriter("C:\Temp\Numbers.txt")

for ($i = 400000000; $i -lt 500000000; $i++) {
   $stream.WriteLine('0' + $i)
}
$stream.Close()

 

http://i.imgur.com/NCA70.png

 

http://i.imgur.com/QVI1e.png

 

* My PC has a Core i5-2500K @ 4.6GHz, 16GB RAM and a 256GB SSD.

Edited by Arthur
  • Thanks 1
Posted

@Arthur thanks for the poweshell method also, but is there any way for it to add the preceding 0?

 

Also if anyone could correct my bash method I wuoldnt mind knowing how to do it in that too please. I tried while, repeat, etc and couldnt seem to get it.

Posted (edited)
Also if anyone could correct my bash method I wuoldnt mind knowing how to do it in that too please. I tried while, repeat, etc and couldnt seem to get it.

 

Short answer, either format outputs to set number of digits with preceeding 0s, or cheat and just echo an extra 0 in front of each :)

 

 echo "0$i"; etc

 

Or if you want use printf with formatting.

 

Edit - Guess you could just do it like this too:

 seq -f %09.0f 0 1000000000 

Formatting it before output, so should it anyway.

Edited by Steve21
Posted
@Arthur thanks for the poweshell method also, but is there any way for it to add the preceding 0?

 

Also if anyone could correct my bash method I wuoldnt mind knowing how to do it in that too please. I tried while, repeat, etc and couldnt seem to get it.

 

 

from man seq:

-w, --equal-width

equalize width by padding with leading zeroes

 

not sure why it crashes, perhaps seq can't deal with so many numbers?

Posted
from man seq:

-w, --equal-width

equalize width by padding with leading zeroes

 

not sure why it crashes, perhaps seq can't deal with so many numbers?

 

Equal width wouldn't affect it. (I think anyway)

 

THat's for use with different sizes,

 

e.g. 1 and 100 because 001 100

 

As the numbers he has are same size it wouldn't change them :(

 

Steve

Posted (edited)
Equal width wouldn't affect it. (I think anyway)

 

THat's for use with different sizes,

 

e.g. 1 and 100 because 001 100

 

As the numbers he has are same size it wouldn't change them :(

 

Steve

 

 

I just did:

seq -w 0400 0499

and it seemed ok

 

you could also do:

!#/usr/bin/python
count = 400000000
while count <= 499999999:
      count = count + 1
      print '0%s' % (count)

Edited by CyberNerd
  • Thanks 2
Posted
I just did:

seq -w 0400 0499

and it seemed ok

 

Ah fair enough, maybe it does it different as you already used the following 0s :( Didn't like it for me!

 

Steve

Posted

!#/usr/bin/python

count = 400000000

while count <= 499999999:

count = count + 1

print '0%s' % (count)

 

that took about 3min to run on Intel® Core™2 Duo CPU T5870 @ 2.00GHz

 

 

tail file2

0499999991

0499999992

0499999993

0499999994

0499999995

0499999996

0499999997

0499999998

0499999999

0500000000

 

 

ls -lh file2

-rw-rw-r--. 1 cybernerd cybernerd 1.1G Jun 30 17:06 file2

Posted

this is either a quad core or its dualcore with hyperthreading but it seems i have 4 cpus

 

task manager shows cmd.exe isnt using more than 12%

 

Ill try it in python later see if it will run faster but im guessing its nearly done. It wont open in np++ any more

Posted
that took about 3min to run on Intel® Core™2 Duo CPU T5870 @ 2.00GHz

 

actually, it takes just over 2min

 

time python ./file > file2

 

real 2m17.287s

user 2m10.026s

sys 0m4.393s

 

 

dos 1 bash 0

 

python 1 powershell 0 :)

Posted

I cant get the python one to work, it just displays 50000000 for me..

 

root@ubuntu:~# ./test.py

0500000000

root@ubuntu:~# cat test.py

#!/usr/bin/python

count = 400000000

while count <= 499999999:

count = count + 1

print '0%s' % (count)

root@ubuntu:~#

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...