seq command in Linux with Examples


Advertisements

Name

seq print a sequence of numbers.

Synopsis

seq [OPTION]... LAST
seq [OPTION]... FIRST LAST
seq [OPTION]... FIRST INCREMENT LAST

Description

seq command is used to print a sequence of numbers from FIRST to LAST in steps of INCREMENT. By default, each number is printed on a separate line. If the FIRST or the INCREMENT is not specified, it defaults to '1'. Even when FIRST is larger than LAST, INCREMENT defaults to '1'.

So seq 1 would output ‘1’, but seq 0 and seq 10 5 would produce no output. The sequence of numbers ends when the sum of the current number and INCREMENT would become greater than LAST, so seq 1 10 10 will only produces ‘1’. Floating-point numbers may be specified as FIRST, LAST or INCREMENT.

Options

-f, --format=FORMAT
   use printf style floating-point FORMAT

-s, --separator=STRING
   use STRING to separate numbers (default: \n)

-w, --equal-width
   equalize width by padding with leading zeroes

--help display this help and exit

--version
   output version information and exit

Examples

$ seq 1 3
1
2
3
$

We can even generate floating point numbers using seq command.

$ seq 2.5 1 6
2.5
3.5
4.5
5.5
$

By default numbers generated by seq command are printed on a new line. But we can use -s option to seperate them using string.

$ seq -s\| 2.5 1 6
2.5|3.5|4.5|5.5
$ seq -s',' 2.5 1 6
2.5,3.5,4.5,5.5
$ seq -s':' 2.5 1 6
2.5:3.5:4.5:5.5
$

Use -w option to print all numbers with the same width, by padding with leading zeros.

$ seq -w 1 50.5 400
001.0
051.5
102.0
152.5
203.0
253.5
304.0
354.5
$ 

Use -f or --format option to print the generated sequence of numbers ia specified FORMAT. FORMAT must contain exactly one of the ‘printf’-style floating point conversion specifications ‘%a’, ‘%e’, ‘%f’, ‘%g’, ‘%A’, ‘%E’, ‘%F’, ‘%G’. The ‘%’ may be followed by zero or more flags taken from the set ‘-+#0 '’, then an optional width containing one or more digits, then an optional precision consisting of a ‘.’ followed by zero or more digits. FORMAT may also contain any number of ‘%%’ conversion specifications.

The example below shows a sequence that is prefexed with 'EEG' with total width of 8 for digits, with 0 padding, and decimal precision of 3.

$ seq -f 'EEG%08.3f' 10 25 200
EEG0010.000
EEG0035.000
EEG0060.000
EEG0085.000
EEG0110.000
EEG0135.000
EEG0160.000
EEG0185.000
Advertisements