Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash number sequence returned in scientific notation?

I'm trying to create a file with all the numbers up 75 million on every line.

I'm typing in my terminal using Bash:

seq 1 75000000 > myfile.csv

But anything above 1 million gets turned into scientific notation and I'd want everything as integer (i.e. 7.31896e+07)

Do you have any idea how to achieve this?

like image 430
FinanceGardener Avatar asked Oct 24 '25 13:10

FinanceGardener


2 Answers

seq can take a printf-style format string to control its output. f format with a "precision" of 0 (i.e. nothing after the decimal point, which leaves off the decimal point itself) should do what you want:

$ seq 74999998 75000000
7.5e+07
7.5e+07
7.5e+07
$ seq -f %1.0f  74999998 75000000
74999998
74999999
75000000
like image 182
Gordon Davisson Avatar answered Oct 27 '25 11:10

Gordon Davisson


Observe that this produces integers:

$ seq  74999998 75000000
74999998
74999999
75000000

While this produces floating point numbers:

$ seq -f '%.5g' 74999998 75000000
7.5e+07
7.5e+07
7.5e+07

The output format of seq is controlled by the -f options.

How is the -f option being applied in your case? One possibility is that your shell has an alias defined for seq that applies this option. For example:

$ alias seq="seq -f '%.5g'"
$ seq 74999998 75000000
7.5e+07
7.5e+07
7.5e+07

You can determine if this is the case by running bash's builtin type command:

$ type seq
seq is aliased to `seq -f '%.5g''

Documentation

From man seq:

   -f, --format=FORMAT
          use printf style floating-point FORMAT
like image 27
John1024 Avatar answered Oct 27 '25 11:10

John1024