Go to content Go to navigation & search

welcome to weekeat.com

Navigation

navigation

feeds

choose: rss / atom


Content

A little utility tool called seq

posted 7 September 2006

While I was exploring various binaries on my Ubuntu, I came across this nifty little utility called seq. A very simple little command that generates a sequence of numbers.

The usage is really easy too. Let’s say you need to generate a sequence of 1 – 5, you can use the following command:

me@mycomp:~$
me@mycomp:~$ seq 1 5
1
2
3
4
5


In the above, the FIRST value, 1, is the begining value of the sequence range and the LAST value, 5, is the limit of the range. You can use any values for the range like seq 50 100, which will generate values starting from 50 and ends at 100.

So.. what’s the bid deal? I don’t know. Anyone can do that with a simple perl or shell script. But I thought that if you know there’s this utility, you can whip it up in less than a second.

That’s not the only thing that it can do. You can specify the increment value, separator string and apply automatic padding. If you’re interested, read on.

Specifying increment value

With the seq command, you can also specify the increment value. Let’s say you want to have a set of value generated from 1 to 10 with an increment of 2, you can do the following:

me@mycomp:~$
me@mycomp:~$ seq 0 2 10
0
2
4
6
8
10


Decoding the above, the FIRST value, 0, is the starting value and the LAST value, 10, is the, well last value and the MIDDLE value, 2, is the increment amount.

So, the above will generate a sequence from 0 to 10, with an incremental value of 2.

Specifying value separator

Well, so far all the numbers are generated with a newline at the end. But what if I want to pipe these values to a file or a script in a string, separated with, let’s say a comma (,)?

No worries. you can do this with the -s option and specifying what you want to use as a separator. So, if you want the values to be separated with commas, you can do the following:

me@mycomp:~$
me@mycomp:~$ seq -s ‘,’ 10 15
10,11,12,13,14,15


You can specify just about anything as separator like pipe (|) or colon (:). It’s up to you.

Padding values with leading zeros

Need the numbers to be padded with zeros like 01,02 … 10 or 001,002 … 100 ? No problem too. You can achieve this by using the -w option like the following:

me@mycomp:~$
me@mycomp:~$ seq -w -s ‘,’ 1 10
01,02,03,04,05,06,07,08,09,10


In the above example, I used the -w option AND the -s ‘,’ option so that the output are separated by commas. You can see that with the -w option, the values are now padded with zeros in front so that all the generated value widths are the same.

Therefore, if you have a range between 1 to 1000, you will have numbers starting with 0001,0002 … 1000 like the following:

me@mycomp:~$
me@mycomp:~$ seq -w -s ‘,’ 1 100 1000
001,101,201,301,401,501,601,701,801,901


In this last example above, I’ve demonstrated the use of all options (yeah, only three) – padded with zeros, used commas as separator and changed the increment value to 100.

What can it be used for? I don’t know yet. But when I do, I’ll post it here. :)

UPDATE: I’ve found a great use of the seq command using the -f option. I’ve documented it here

Hope you find it useful.

 
Comment by: Ronny (Sep 8, 00:10)

I just used it today to help generate one million line of SQL inserts for random test data.

Before I found out about seq, I used to use a perl one-liner to generate sequence, e.g.:
perl -e 'print"$_\n"for(1..10)'

Comment by: Wee Keat Chin (Sep 16, 16:29)

Cool Ronny. I’ve just got seq to generate a million email too using the -f command (refer to this entry) and it only took about 2 seconds.

Comment by: vyvyan (Nov 3, 04:48)

here is an example to rename files generated by shnsplit using cue sheet

for a in `seq -w 1 $(cueprint -d %N *.cue)`;do mv -v “split-track$a.mp3” “$(cueprint -n $a -t “%02n. %t.mp3” *.cue)”; done

Got something to say?

  Textile Help