Use the Linux seq command to generate a sequence of numbers (recommended)

Use the Linux seq command to generate a sequence of numbers (recommended)

The Linux 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 (sequence) command. In its simplest form, seq takes a number as an argument and outputs a list from 1 to that number. For example:

$ seq 5
1
2
3
4
5

Unless otherwise specified, seq always starts with 1. You can start a sequence by inserting a different number before the final number.

$ seq 3 5
3
4
5

Specify Increment

You can also specify the increment step. Suppose you want to list the 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

You can choose to use negative increments (i.e. decrements) to go from larger to smaller numbers.

$ seq 18 -3 3
18
15
12
9
6
3

The seq command is also very fast. You could probably 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.899s

Using Delimiters

Another very useful option is to use a separator. Instead of listing a single number on each line, you can insert a comma, colon, or some other character. The -s option is followed by the character to be used.

$ seq -s: 3 3 18
3:6:9:12:15:18

In fact, if you just want the numbers to fit on one line, you can use spaces instead of the default line breaks.

$ seq -s' ' 3 3 18
3 6 9 12 15 18

Start math

It may seem like a big leap to go from generating sequences of numbers to doing math with them, but with the right delimiter, seq can easily be passed to bc for calculations. For example:

$ seq -s* 5 | bc
120

What happened in this command? let's see. First, seq generates a list of numbers using * as the delimiter.

$ seq -s* 5
1*2*3*4*5

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 0m0.003s
user 0m0.004s
sys 0m0.000s

limitation

You can only choose one delimiter, so the calculations will be very limited. Using bc alone can perform more complex mathematical operations. Also, seq only works with numbers. To generate a sequence of single letters, use the following command instead:

$ echo {a..g}
abcdefg

Summarize

The above is what I introduced to you about using the Linux seq command to generate digital sequences. I hope it will be helpful to you. If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!
If you find this article helpful, please feel free to reprint it and please indicate the source. Thank you!

You may also be interested in:
  • How to use the Linux seq command
  • Detailed explanation of the use of Linux seq command

<<:  Mini Programs use Mini Program Cloud to implement WeChat payment functions

>>:  A Brief Analysis of the Differences between “:=” and “=” in MySQL

Recommend

Sharing ideas on processing tens of millions of data in a single MySQL table

Table of contents Project Background Improvement ...

CentOS7.x uninstall and install MySQL5.7 operation process and encoding format modification method

1. Uninstalling MySQL 5.7 1.1查看yum是否安裝過mysql cd y...

Implementation of waterfall layout in uni-app project

GitHub address, you can star it if you like it Pl...

202 Free High Quality XHTML Templates (2)

Following the previous article 202 Free High-Qual...

Detailed explanation of the murder caused by a / slash in Nginx proxy_pass

background An nginx server module needs to proxy ...

Solve the MySQL 5.7.9 version sql_mode=only_full_group_by problem

MySQL 5.7.9 version sql_mode=only_full_group_by i...

About the use of Vue v-on directive

Table of contents 1. Listening for events 2. Pass...

How to run Spring Boot application in Docker

In the past few days, I have studied how to run s...

Node.js file copying, folder creation and other related operations

NodeJS copies the files: Generally, the copy oper...

A simple way to call desktop exe programs on a web page

This article mainly introduces how to call desktop...

How to solve the 2002 error when installing MySQL database on Alibaba Cloud

The following error occurred while installing the...

How to insert pictures into HTML pages and add map index examples

1. Image formats supported on the WEB: GIF: can s...