How to use seq to generate a sequence of numbers in Linux

0


[ad_1]

In Linux, you can find several commands with unusual functionality. One of these commands is seq, which generates a sequence of numbers based on the specified arguments.

But what can you do with a command line utility that throws you a bunch of numbers? You will find out in this guide.

What is the seq command?

As mentioned above, the seq command on Linux quickly generates a sequence of numeric characters. Users can pass arguments to the command to generate different combinations of numbers. For example, you can get an incremented list by simply passing an additional argument to seq.

What is the practical use of the command though? Although seq may not seem like a powerful tool in its entirety, you can benefit from the command by implementing it with other Linux utilities. You can also use seq in bash scripts to reveal its true power.

How to use seq on Linux

Seq only takes a few arguments, making it easy to learn for anyone.

Basic syntax

The basic command syntax is:

seq options numbers

…or options are the flags you can specify to call different methods of the command and Numbers are the arguments you pass to generate the numeric sequence.

Generate a list of numbers

Seq arguments follow the input format below:

seq last
seq first last
seq first increment last

When you specify only one number, seq interprets it as the upper limit of the list and generates a sequence from one to the specified number.

seq 5

The aforementioned command will display the following:

1
2
3
4
5

When seq receives two numbers as input, it interprets them as the lower limit and the upper limit of the sequence. To generate a list of numbers from four to eight:

seq 4 8

Production:

4
5
6
7
8

But when you pass three numbers to the command, it interprets the second argument as the number of increments. For example:

seq 3 2 13

The aforementioned command will display a list of numbers ranging from three to 13 with an increment of two.

3
5
7
9
11
13

Add a separator between numbers

By default, seq uses a newline character as the list separator. This is the reason why each number in the list is on a separate line.

You can change this default behavior and use a custom separator using the -s flag. To use the Period (.) character as separator:

seq -s . 3 7

Production:

3.4.5.6.7

Keep in mind that some characters like the Tilde (~) must be enclosed in quotation marks. This is because the terminal uses the Tilde character to designate the /home directory, and that would be reflected in the output if you didn’t add the quotes.

seq -s ~ 3 7

Production:

3/home/4/home/5/home/6/home/7

On the other hand, when you surround the separator in quotes:

seq -s '~' 3 7

Production:

3~4~5~6~7

Adjust output format

You can also change the format of the output sequence using the -F flag. By default, seq extracts the format style from user input. For example, if you specify the numbers 0.1 and 0.5, the output will default to a floating point number format.

seq 0.1 0.5

Production:

0.1
0.2
0.3
0.4
0.5

You can specify a custom output format using the various conversion specifications such as% a,% e,% f,% g,% A,% E,% F, and% G.

You can use the % F specify whether you want the output to follow a floating point number format.

seq-f %f 4 7

Production:

4.000000
5.000000
6.000000
7.000000

To change the precision up to two decimal places:

seq -f %0.2f 4 7

Production:

4.00
5.00
6.00
7.00

You can also completely transform the output by specifying an output template. For example, to get a list of all IP addresses starting with 192.168.5.x:

seq -f 192.168.5.%g 1 233

Production:

custom output format seq

To add padding to the output, you can use the -w flag. the -w flag maintains the width of the output according to the largest number specified.

To generate a sequence of numbers between one and 1000 with an increment of 100 while keeping the width constant:

seq -w 1 100 1000

Production:

0001
0101
0201
0301
0401
0501
0601
0701
0801
0901

Get seq command line help

Although seq is easy to use, sometimes users may feel the need to consult the manual page for the command. the –help flag will display the seq man page:

seq --help

Production:

linux seq man page

Practical examples

As already mentioned, seq is mainly used with other Linux commands, for example, touch and expr.

Perform mathematical operations

If you want to quickly add or subtract a particular range of numbers, you can do that easily by using seq inside expr, which is a Linux command that treats the input as an expression and displays the corresponding output.

To add all the numbers between one and 100:

expr `(seq -s " + " 1 100)`

The seq command generates output as follows:

1 + 2 + 3 + 4 + 5 + 6...

Expr treats it as an input expression and generates the solution.

5050

You can perform other math operations by simply replacing the separator in the seq command with other operators.

Quickly create multiple files

If you want to create multiple files on Linux whose names follow a similar pattern, you can do so easily by using the touch and seq command.

For example, to create 10 files with the name file-x.txt, or X is a number from 1 to 10:

touch $(seq -f "file%g.txt" 1 10)

Touch will create the files for you in a flash.

quickly create files with touch and seq

Implementing seq in scripts

Considering that you are writing a network scan tool like Nmap in bash, you might want to get a list of all open ports in a network. But for that, you need to ping each port (65535 in total) and analyze the response.

To save time, you can choose to use seq and generate a list of IP addresses and port combinations that you can use in your script.

Suppose you want to get the list of all the ports of a device with the IP address 1.2.3.4. Here is a quick command to generate the desired output:

seq -f 1.2.3.4:%g 1 65535

Production:

use seq in a bash script

You can then use that output as a list and iterate through it, checking each port using your script and analyzing whether it is open or not.

How fast does seq generate the numbers?

You might be thinking that if you can get similar results using a for loop in bash, why choose seq for the task? This is because the real power of seq lies in its speed. Seq is faster than any other command that generates a sequence of numbers on Linux.

You can even test its speed using the time utility on Linux. Let’s see how long does it take for seq to generate a list of a million numbers from one.

time seq 1000000

Looking at the output below, you can see that it only took about four seconds to generate a list of a million numbers.

Linux sequence speed

The power of the Linux command line

Seq is not the only tool in Linux that has a strong focus on delivering fast and accurate results. Although you can generate a list of numbers using a for loop in bash, this is not a recommended practice given how fast seq really is.

The Linux command line gives you more control over the operating system and its functionality, which is also a reason why you should start using the terminal through the GUI today.


choose the linux terminal rather than the graphical interface

5 reasons to choose the Linux terminal over the GUI

The Linux command line is of great importance in terms of performance, control, and ease of use.

Read more


About the Author

.

[ad_2]

Share.

Leave A Reply