Trix & Graphix

cat and tac

Unix-like system are based on a very simple set of ideas, one of them is: every program has to solve just one task, and do it right. Well, this is exactly what does one of the most common tools in Linux: cat.

Many people use cat just for getting the output from an ascii file, in commands like

$ cat file.asc
Well, open a file, decode the ascii format and print out a human-readable version in screen is a great thing, but it is not the only thing cat can do.

cat may be used to concatenate files, independently of their format. For example
$ cat file1.asc file2.asc > files1-2.asc
joins both files in another ascii format file.

Not only ascii files, but it also work with binary ones, for example with metheorological EXTRA format
$ cat file1.ext file2.ext > files1-2.ext
But the most powerful feature of cat is that it is a text editor. To use it, you have to type:
$ cat > file.txt << EDIT
some text

you want to add
into your file
EDIT
which means something like "take the input of the keyboard and insert it in file.txt. Wait until I type again EDIT, which will mean I want you to stop listening".

This feature is extremely useful for scripting purposes, as you can make scripts which create other scripts and so on. May be I will put some example of this in a (I hope) near future.

Finally, there exists another command, which I found out few time ago, tac. This programs do exactly the same as cat, but from bottom to up. This is, tac may be used for printing a file reversing the order of rows. It may be very useful when you know that the interesting part is at the end of a file.

BTW. Many people use the command:
$ cat file.txt | grep something
trying to look for "something" in the file. There is no point to do that. grep is another tool which does not need at all cat to work. The correct form would be:
$ grep something file.txt