0

I know that I can use the seq command to generate a sequence of numbers, such as:

seq 100 999

...and I know I can create a file with the addition of:

seq 100 999 > file.txt

...but what if I wanted to perform a calculation on each number, before writing it to a file?

I want to basically create a file of numbers, which contain the results of:

seq  100 999 x(times) date +%s > file.txt

I know this isn't the way to do it, but I'm curios about how it could be done.

Ultimately, the numbers that are created will be serial numbers, which can never (ever) be duplicated. The resulting numbers will actually need to be added to a MySQL database (not a file.txt file) and I will need to add more numbers to said MySQL database on a hourly/daily basis.

Any help/suggestions would be appreciated.

  • 1
    Would it not be easier to use a database sequence type so that new numbers are generated on demand by MySQL? Or do the numbers specifically need to be non-monotonically increasing? – John Oct 06 '15 at 18:42
  • be careful that you don't overlap the upper-end (999 * now) with a future lower-end (100 * future). Random is different from serialized... – Jeff Schaller Oct 06 '15 at 19:12
  • @JeffSchaller - This will be used in a manufacturing environment. The number sequence will never go down; only up. So I may start with 100 - 999 as my first thousand numbers, but the next thousand would be 1000-1999 and so on. – WebDevRobert Oct 06 '15 at 19:48
  • @John - A file does need to be created for use with an industrial barcode printer. However, it does not matter whether the numbers are generated in a MySQL database first and then exported as a file, or vice-versa. – WebDevRobert Oct 06 '15 at 19:52

2 Answers2

2

you can use seq, xargs and expr as:

seq 100 999|xargs -n1 expr $(date +%s) \*

the -n1 option tells xargs to execute expr with one element of the sequence at a time. The $(date ...) expression will only be evaluated once.

adonis
  • 1,724
0

Is Perl ok? For every number in the range 100-999, it multiplies that number with perl function time() to it (which is the same as doing date +%s).

perl -E 'for(100..999){say $_ * time();}' > file.txt

1438382172768
1439826331576
1441270490384
1442714649192

etc.
stevieb
  • 886
  • Perl is fine... Thank you very much for your response, but I want to multiply the generated number by the time stamp. – WebDevRobert Oct 06 '15 at 19:11
  • I've updated my answer to do exactly that. – stevieb Oct 06 '15 at 19:15
  • I did test that, before you commented, and have seen that it does work. I believe that to be the answer to my initial question and have marked it as such. However, can you tell me if the time() expression is being evaluated with every number, or only once? – WebDevRobert Oct 06 '15 at 19:16
  • It is called on every iteration of the for() loop, so it's a new timestamp for every number within the range. – stevieb Oct 06 '15 at 19:17
  • You can verify that by running this: perl -E 'for(100..999){sleep 1; say time();}' – stevieb Oct 06 '15 at 19:20
  • Fantastic. Is there anything special you can think of, which would need to be done, in order for the output to be a valid .csv file? – WebDevRobert Oct 06 '15 at 19:35