[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

25.2 seq: Print numeric sequences

seq prints a sequence of numbers to standard output. Synopses:

 
seq [option]... [first [increment]] last...

seq prints the numbers from first to last by increment. By default, first and increment are both 1, and each number is printed on its own line. All numbers can be reals, not just integers.

The program accepts the following options. Also see 2. Common options.

`-f format'
`--format=format'
Print all numbers using format; default `%g'. format must contain exactly one of the floating point output formats `%e', `%f', or `%g'.

`-s string'
`--separator=string'
Separate numbers with string; default is a newline. The output always terminates with a newline.

`-w'
`--equal-width'
Print all numbers with the same width, by padding with leading zeroes. (To have other kinds of padding, use `--format').

If you want to use seq to print sequences of large integer values, don't use the default `%g' format since it can result in loss of precision:

 
$ seq 1000000 1000001
1e+06
1e+06

Instead, you can use the format, `%1.f', to print large decimal numbers with no exponent and no decimal point.

 
$ seq --format=%1.f 1000000 1000001
1000000
1000001

If you want hexadecimal output, you can use printf to perform the conversion:

 
$ printf %x'\n' `seq -f %1.f 1048575 1024 1050623`
fffff
1003ff
1007ff

For very long lists of numbers, use xargs to avoid system limitations on the length of an argument list:

 
$ seq -f %1.f 1000000 | xargs printf %x'\n' | tail -n 3
f423e
f423f
f4240

To generate octal output, use the printf %o format instead of %x. Note however that using printf works only for numbers smaller than 2^32:

 
$ printf "%x\n" `seq -f %1.f 4294967295 4294967296`
ffffffff
bash: printf: 4294967296: Numerical result out of range

On most systems, seq can produce whole-number output for values up to 2^53, so here's a more general approach to base conversion that also happens to be more robust for such large numbers. It works by using bc and setting its output radix variable, obase, to `16' in this case to produce hexadecimal output.

 
$ (echo obase=16; seq -f %1.f 4294967295 4294967296)|bc
FFFFFFFF
100000000

Be careful when using seq with a fractional increment, otherwise you may see surprising results. Most people would expect to see 0.3 printed as the last number in this example:

 
$ seq -s' ' 0 .1 .3
0 0.1 0.2

But that doesn't happen on most systems because seq is implemented using binary floating point arithmetic (via the C double type) -- which means some decimal numbers like .1 cannot be represented exactly. That in turn means some nonintuitive conditions like .1 * 3 > .3 will end up being true.

To work around that in the above example, use a slightly larger number as the last value:

 
$ seq -s' ' 0 .1 .31
0 0.1 0.2 0.3

In general, when using an increment with a fractional part, where (last - first) / increment is (mathematically) a whole number, specify a slightly larger (or smaller, if increment is negative) value for last to ensure that last is the final value printed by seq.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated by Jeff Bailey on December, 28 2002 using texi2html