… and I’m not talking about Ceiling Cat. I’m talking about the Linux command cat.
Apropos the idea that one should not use cat to produce a stream of text for a command that takes a filename as a argument … I say “Balderdash!”
If your plan is to process the text from a file with a single command and that’s that .. no modifications will be needed … then by all means, you will save time and computing resources (though immeasurably little of either) by using the filename as an argument to said command.
But if you are not quite sure how you are going to get to your eventual final results, you might find that staring with cat will give you a paws up when you need to start fiddling. The cat command will even take arguments that can help you.
In fact, I think it is the widespread conception that cat is ‘merely a filter’ that does nothing but pass the contents of the file it is fed contributes to the belief that cat is always useless. In fact, cat is a very powerful command.
Cat counts as a filter. A file goes in and comes out. When cat is issued with nothing other than a filename as an argument, it does nothing but stream out the contents of the file. This is convenient if you want to construct a complex command that starts with the contents of a file running into standard input. For instance:
will simply ‘print’ to the terminal the contents of moue.txt. But that’s OK. You can do this and verify that mouse.txt exists and contains roughly what you thought it contained. Then you can add something to this such as
cat mouse.txt | grep mice
which gets me:
Three blind mice.
Three blind mice.
As three blind mice.
I could have gone
grep mice mouse.text
and gotten the same thing, but that would interfere with the poetry of
cat mouse.txt | grep mice | wc
which gets me: 2 10 66
which is, of course, either Daryl Johnston’s or Rodney Anoa’i's birthday.
Here’s another good one. A common question given to programmers looking for work, as part of their application, is this:
“Write a perl one liner that adds numbers to the lines in a file.”
Who needs perl? … cat -n mouse.txt
which gets me this:
1 Three Blinde Mice,
2 three Blinde Mice,
3 Dame Iulian,
4 Dame Iulian,
5 The Miller and his merry olde Wife,
6 shee scrapte her tripe licke thou the knife.
8 Three blind mice. Three blind mice.
9 See how they run. See how they run.
10 They all ran after the farmer’s wife
11 She cut off their tails with a carving knife.
12 Did you ever see such a thing in your life
13 As three blind mice.
or, cat -b mouse.txt
which is similar but only numbers non-blank lines.
or the -s option, one of the coolest, which does not allow more than one blank line at a time through the filter!
or -T which shows tabs, otherwise invisible.
Man cat. Try it, you’ll like it.