Seq: Difference between revisions
From charlesreid1
| Line 114: | Line 114: | ||
10 | 10 | ||
</source> | |||
==Custom String Terminator== | |||
<source> | |||
$ seq -t'\n...' 0 2 10 | |||
0 | |||
2 | |||
4 | |||
6 | |||
8 | |||
10 | |||
... | |||
</source> | |||
==Zero-Padding== | |||
Sometimes it's useful to have a fixed-width number, with zero-padding. That's why seq offers the -w (for width) option: | |||
<source> | |||
$ seq -w 0 2 10 | |||
00 | |||
02 | |||
04 | |||
06 | |||
08 | |||
10 | |||
</source> | |||
==Complex String Formatting== | |||
You can use the -f flag to format your string using printf functionality. | |||
To print a 4-digit integer, with zero padding, you'll use the <code>%04g</code> printf sequence: | |||
<source> | |||
$ seq -f'%04g' 0 2 10 | |||
0000 | |||
0002 | |||
0004 | |||
0006 | |||
0008 | |||
0010 | |||
</source> | |||
To print a 4-digit integer with spaces for padding, you can use the <code>%4g</code> printf sequence: | |||
<source> | |||
$ seq -f'%4g' 0 2 10 | |||
0 | |||
2 | |||
4 | |||
6 | |||
8 | |||
10 | |||
</source> | |||
To print the sequence of numbers with a prefix, you can add a prefix to the printf sequence, e.g.: | |||
<source> | |||
$ seq -f'_%04g' 0 2 10 | |||
_0000 | |||
_0002 | |||
_0004 | |||
_0006 | |||
_0008 | |||
_0010 | |||
</source> | </source> | ||
=Combining Seq with Other Unix Utilities= | =Combining Seq with Other Unix Utilities= | ||
Latest revision as of 20:06, 9 January 2014
The seq command can be used for creating numerical sequences of numbers.
Basic Use of Seq
If you want a list of numbers from 1 to 10:
$ seq 1 10
1
2
3
4
5
6
7
8
9
10or, if you want a backwards list:
$ seq 10 1
10
9
8
7
6
5
4
3
2
1Decimal Numbers
Seq can handle decimal increments as well:
$ seq 0 0.5 5
0
0.5
1
1.5
2
2.5
3
3.5
4
4.5
5Custom Increment
If you want a sequence of numbers from A to B with a particular increment i, feed three arguments to seq: seq A i B
$ seq 0 5 50
0
5
10
15
20
25
30
35
40
45
50You can also give negative increments,
$ seq 50 -5 0
50
45
40
35
30
25
20
15
10
5
0Custom Separator
Creating a comma-separated list of numbers is often more useful/interesting than each number on a separate line:
$ seq -s, 0 2 10
0,2,4,6,8,10,You can also use escape characters, like a double-newline:
$ seq -s'\n\n' 0 2 10
0
2
4
6
8
10Custom String Terminator
$ seq -t'\n...' 0 2 10
0
2
4
6
8
10
...Zero-Padding
Sometimes it's useful to have a fixed-width number, with zero-padding. That's why seq offers the -w (for width) option:
$ seq -w 0 2 10
00
02
04
06
08
10Complex String Formatting
You can use the -f flag to format your string using printf functionality.
To print a 4-digit integer, with zero padding, you'll use the %04g printf sequence:
$ seq -f'%04g' 0 2 10
0000
0002
0004
0006
0008
0010To print a 4-digit integer with spaces for padding, you can use the %4g printf sequence:
$ seq -f'%4g' 0 2 10
0
2
4
6
8
10To print the sequence of numbers with a prefix, you can add a prefix to the printf sequence, e.g.:
$ seq -f'_%04g' 0 2 10
_0000
_0002
_0004
_0006
_0008
_0010