generate and print a sequence of number

September 28th, 2006 mysurface Posted in backquote, cat, Misc, pipeline, seq, Text Manipulation, wc | Hits: 98972 | 1 Comment »

To generate a sequence of number we can use seq

seq 1 10

With this it prints 1 to 10 line by line, because by default separator is new line, anyway you can check, do this

seq -s" " 1 10

Doing this it change new line to a single blank space.

But that is the usage of the this seq ? Let me give an usage example.

Let say I have few text files, all these text files contain lines of book name. All those books name is kept in separate files base on the year published. So 1990.txt contains books published at year 1990.

Now I want to calculate how many books I have in year 1990 to 1998, what should I do ?

cat `seq -f "%g.txt" -s" " 1990 1998` | wc -l

-f is to specified the format of each generated numbers, it works like printf in c. wc -l is to count the lines. I cat all files from 1990.txt til 1998.txt and pass it to count the lines, with that I successfully count all the books.

One Response to “generate and print a sequence of number”

  1. another *wicked* alternative for the “calculate how many books I have” example above:

    cat `ls` | wc -l

Leave a Reply