Some command line tools may seem limited at first glance, but when combined with others, their true capabilities shine. One such tool is seq, which is designed to print a sequence of numbers. This tutorial explores the basic usage of the seq command with clear examples.
All examples in this guide have been tested on an Ubuntu 16.04 system.
Linux seq Command
The seq command is used to print a sequence of numbers. Here’s the syntax:
seq [OPTION]... LAST seq [OPTION]... FIRST LAST seq [OPTION]... FIRST INCREMENT LAST
According to the tool’s man page:
Print numbers from FIRST to LAST, in steps of INCREMENT. If FIRST or INCREMENT is omitted, it defaults to 1. An omitted INCREMENT defaults to 1 even when LAST is smaller than FIRST. The sequence of numbers ends when the sum of the current number and INCREMENT would become greater than LAST. FIRST, INCREMENT, and LAST are interpreted as floating-point values. Typically, INCREMENT is positive if FIRST is smaller than LAST, and negative if FIRST is greater than LAST. FORMAT should be suitable for printing one argument of type 'double'; it defaults to %.PRECf if FIRST, INCREMENT, and LAST are all fixed point decimal numbers with maximum precision PREC, and to %g otherwise.
Below, you’ll find some Q&A-styled examples to clarify how the seq command works.
Q1. How Does the Seq Command Work?
Using seq is straightforward. Provide a number, and the command will output numbers from 1 to that number.
Example:
seq 8
Additionally, you can specify the starting number for your sequence.
Example:
seq 3 8
You can also define the increment step, which defaults to 1. For instance, to print from 1 to 9 with a step of 2:
seq 1 2 9
Q2. How to Add a Separator?
To enhance the seq output with a separator, use the -s option.
Example, using a comma as a separator:
seq -s, 1 9
Q3. How to Specify Output Format?
The seq command allows output formatting similar to printf through the -f option. Details are provided in the tool’s info page:
`-f FORMAT' `--format=FORMAT' Print all numbers using FORMAT. FORMAT must contain exactly one printf-style floating point conversion specification such as `%a', `%e', `%f', `%g', or their uppercase variants. The `%' may be followed by flags `-+#0 '', an optional width, and an optional precision consisting of a `.' followed by digits. FORMAT may also include `%%' for literal percent signs. All behave as in `printf'.
The default format is `%g’, unless all numbers use fixed point decimal representation, in which case it defaults to `%.Pf’, with P being the minimal precision needed for accuracy.
Example usage:
seq -f "%02g" 6
Q4. How to Use Seq with Other Commands? (Use Case 1)
If you need to sum numbers, such as from 111 to 121, you can combine seq with other commands like this:
expr `seq -s " + " 111 121`
Illustrated example:
Q5. How to Use Seq with Other Commands? (Use Case 2)
To create multiple files with numerically incremented names (e.g., file1, file2, etc.), you can do:
touch $(seq -f “file%g” 1 10)
Conclusion
The seq command is indeed a powerful tool for generating number sequences efficiently. It has a gentle learning curve, making it accessible for tasks requiring sequence generation. The examples provided here should help you begin using seq effectively. For further details, consult the man page.
Frequently Asked Questions (FAQ)
What is the default increment for seq?
The default increment is 1.
Can seq handle floating-point numbers?
Yes, seq interprets FIRST, INCREMENT, and LAST as floating-point values.
How can I reverse the sequence?
You can reverse the sequence by specifying a negative increment. For example: seq 5 -1 1
.
What happens if the increment is omitted?
If omitted, the increment defaults to 1, even if LAST is smaller than FIRST.