Linux’s seq command can generate lists of numbers at lightning speed, and it’s also easy to use and flexible.

One of the easiest ways to generate a list of numbers in Linux is to use the seq (series sequence) command. In its simplest form, SEQ takes a numeric parameter and prints a list from 1 to that number. Such as:

$ seq 5
1
2
3
4
5
Copy the code

Unless otherwise specified, seQ always begins with 1. You can start a sequence by inserting different numbers in front of the final number.

$ seq 3 5
3
4
5
Copy the code

Specify the incremental

You can also specify incremental steps. Suppose you wanted to list multiples of 3. Specify the starting point (the first 3 in this example), the increment (the second 3), and the end point (18).

$ seq 3 3 18
3
6
9
12
15
18
Copy the code

You can choose to use negative increments (decrement) to reduce the number from large to small.

$ seq 18 -3 3
18
15
12
9
6
3
Copy the code

The seq command is also very fast. You might be able to generate a list of a million numbers in 10 seconds.

$time seq 1000000 1 2 3... ... 999998 999999 1000000 real 0m9.290s <== 9+ seconds user 0m0.020S sys 0m0.899sCopy the code

Use delimiters

Another very useful option is to use delimiters. Instead of listing individual numbers on each line, you can insert commas, colons, or other characters. The -s option is followed by the character to use.

$ seq -s3 3 18 3 6:9 12:15:18Copy the code

In fact, if you just want to list numbers on a single line, you can use Spaces instead of the default newline character.

$ seq -s' '3, 3, 18, 6, 9, 12, 15, 18Copy the code

Start the math

It might seem like a big leap to go from generating a sequence of numbers to doing the math, but with the right delimiter, SEQ can easily be passed to BC for calculation. Such as:

$ seq -s* 5 | bc
120
Copy the code

What happens in this command? Let’s take a look. First, SEQ generates a list of numbers and uses * as a separator.

$ seq -s5 * 1 * 2 * 3 * 4 * 5Copy the code

It then passes the string to a calculator (BC), which immediately multiplies the numbers. You can do pretty massive calculations in less than a second.

$ time seq -s* 117 | bc 39699371608087208954019596294986306477904063601683223011297484643104\ 22041758630649341780708631240196854767624444057168110272995649603642 \ 560353748940315749184568295424000000000000000000000000000 real 0 m0. 003 s user 0 m0. 004 s sys 0 m0. The 000 sCopy the code

limitations

You can only choose one separator, so the calculation is very limited. BC alone can do more complex math. Also, SEQ only works with numbers. To generate a single letter sequence, use the following command instead:

$ echo{a.. g} a b c d e f gCopy the code

Via: www.networkworld.com/article/351…

Author: Sandra henry-stocker lujun9972

This article is originally compiled by LCTT and released in Linux China