
El Linux seq
The command generates lists of numbers in the blink of an eye. But, How can this functionality be implemented? We'll show you how seq can help you.
The seq command
At first sight, Linux seq
The command seems to be somewhat strange. It enables you to generate number sequences quick and ready! Despite this, the keyword here is “quickly”. In a moment, you will see how fast this little command can be executed.
Regardless of how they are generated, How useful is a list of numbers? the seq
The command was added to the 8th edition of Unix in 1985. Been there ever since, so you must do something worthwhile.
the Unix philosophy is that it is full of small utilities that do one thing and do it well. One of the central principles of this philosophy is to write programs that accept information from other programs.. In any case, This also means that these programs have to generate an output that can be used as input by other programs..
the seq
The command comes into play when used with other commands that make use of its output, either via pipeline or command line expansion.
Basic list generation
If you throw seq
with a single number as a command line parameter, count from one to that number. Later print the numbers in the terminal window, one number per line, as it's shown in the following:
seq 6
If you type two numbers on the command line, the first will be the starting number and the second will be the ending number, as it's shown in the following:
seq 4 10
You can determine a step size by including a third number. It is between the start and end numbers. We write the following to ask seq
to create a list of numbers starting with six, finished in 48 and use a step of six:
seq 6 6 48
Counting backwards
We can also ask seq
to create a list of numbers from highest to lowest. Despite this, to do it, we must take a step that is negative.
The following command produces a list that counts from 24 a 12 in steps of 6 because we write the step as a negative number:
seq 24 -6 12
Counting with decimals
The starting numbers, final and step can also be decimals. If any of the numbers is decimal, the others are also treated as decimals. The following command generates a list of numbers with a step of 0.2:
seq 1 0.2 2
Speed of seq
seq
it's incredibly fast; the only bottleneck is the time it takes to type the command in the terminal window. To test your speed, let's ask for a list of 250.000 numbers.
We write the following, using the time
command to see how long the procedure takes to complete:
time seq 250000
Results are displayed below the list. Even on our moderate power test PC, seq
it's surprisingly fast.
The entire list was created and written to the screen in approximately 1/3 second. If we redirect the list to a file, we can even avoid the overload of writing in the terminal window.
To do it, we write the following:
time seq 250000 > numbers.txt
The time it takes to complete the list and create the file is now approximately 1/7 second.
Using a separator
A newline character is the default character that is displayed between each number in a list. That is why they appear as a vertical list, with each number on its own line. If required, can provide another separator.
As an example, suppose you need to create a comma delimited list, a list divided by a colon or any other punctuation mark or symbol. The delimiter is actually a string, so you can use more than one character.
We will use the -s
(separator) option. The following command will produce a comma delimited list:
seq s, 6 6 36
This command will use a colon (:
) as separator:
seq -s: 6 6 36
This command says seq
use two hyphens-
) as separator:
seq -s-- 6 6 36
Use format strings
the seq
the command is also supported Language style C format strings. These allow you to format the output with much more control than simply specifying a separator.. To use a format string, use the -f
option (format).
The following command says seq
to use zeros to pad the output to two characters:
seq -f "%02g" 6
We can format the string with any text we like and place the number anywhere in the string, in the next way:
seq -f "Number g in a C-like format string" 6
A quick way to set up zero padding
The quickest way to determine the zero padding is to use the -w
(equal width) option. This says seq
use zeros to fill in numbers, so that they are all the same width as the largest number.
The following command counts 0 a 1000 in steps of 100, and all numbers will be padded with zeros:
seq -w 0 100 1000
The longest number has four characters, so all the narrower numbers are padded with zeros up to that width (inclusive 0 is padded with four zeros).
Pipe seq in bc
By determining the separator as a mathematical symbol and pipelining the list into the bc
command, we can evaluate the numbers in that list.
The following command generates a list of numbers separated by asterisks (*
), starting at one and ending with six:
seq -s* 6
If we feed that list on bc
, evaluate the list using the asterisks (*
) as multiplication symbols:
seq -s* 6 | bc
We can also do this with other symbols. The following command uses a plus sign (+
) to create a list in which all numbers are added:
seq -s+ 5
We wrote the following to channel that into bc
and browse the list:
seq -s+ 5 | bc
Create files with seq
the touch
command update date and time stamps in files. If the file does not exist, touch create it. We can use command line expansion with touch
Y seq
to create a collection of files with thematic names but differently numbered.
We will create a set of 10 files with the same base name and a different number (archivo-1.txt, archivo-2.txt, etc.). We write the following:
touch $(seq -f "file-%g.txt" 1 10)
Subsequently, we write the following to verify the files:
ls file*
Using seq in Bash Loops
We can use seq
in bash scripts to control loops with decimals.
type the following text in an editor and then save it as “loops.sh”:
#!/bin/bash for val in $(seq 5 0.2 6.6); do echo "The value is now: $Val" done
Then, we write the following so that our new script is executable:
chmod +x loop.sh
When we run the script, the loop counter is printed in the terminal window. Subsequently, we can type the following to see that the decimal loop counter increases with each iteration of the loop:
./loop.sh
Remember seq
you can also count backwards; you can use that in loops in the same way.
Nice and simple
One thing about seq
is that there is not much learning curve. Have a refreshingly short man
page, but you can still use it in interesting ways.
Because we often need to quickly create realistic sized test files, we use seq
with a format string. Later we redirect the output to create a file that contains as many lines of dummy data as we want.