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:
Unless otherwise specified, seq always starts with 1. You can start a sequence by inserting a different number before the final number.
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).
You can choose to use negative increments (i.e. decrements) to go from larger to smaller numbers.
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.
In fact, if you just want the numbers to fit on one line, you can use spaces instead of the default line breaks.
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:
What happened in this command? let's see. First, seq generates a list of numbers using * as the delimiter.
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:
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! You may also be interested in:
|
<<: Mini Programs use Mini Program Cloud to implement WeChat payment functions
>>: A Brief Analysis of the Differences between “:=” and “=” in MySQL
/****************** * Linux kernel time managemen...
1: django-admin.py startproject project name 2: c...
During the daily optimization process, I found a ...
Table of contents 1. Test environment 1.1 Install...
1. Summary of location usage Location can locate ...
Preface JSON is a lightweight data exchange forma...
Table of contents Preface 1. Arrange the installa...
1. Check whether port 80 is occupied. Generally, ...
SQL is the main trunk. Why do I understand it thi...
Preface There are often some articles on the Inte...
Table of contents Introduction Instructions Actua...
Project scenario: 1. Upload file restrictions Fun...
Table of contents Add Configuration json configur...
Summarize 1. Similarities Both can change the int...
MySQL query by year, month, week, day group 1. Qu...