Now on ScienceBlogs: Must Read

ScienceBlogs Book Club: Inside the Outbreaks

Greg Laden's Blog

Evolution, Life Sciences, Science Education, Human Evolution, and Stuff

Darwing_Face.jpg Learn more about Charles Darwin and his work.

Hornbill170.jpg Looking for stuff about birds?

Lion_mane170.jpg Lean more about lions

Congo_sidebar.jpg An archaeological expedition to the Congo


The Skeptical Search Engine


Nature Blog Network
Climate Defense Fund


The contents of Greg Laden's Blog are copyrighted by Greg Laden.

Recent Comments

Search

Profile


Click on "About" for the big picture, and "Archives" for the details.


Recent Posts

Blogroll

If you don't see yourself on my blogroll, just drop me a line and let me know. I'll add you.*
*Assuming that I'm on your blogroll, of course!

Archives

« A Love Letter to Louise | Main | We have been hit with a fairly serious tornado. »

In the beginning there was cat ...

Category: Computer TricksLinuxone-liners
Posted on: August 19, 2009 4:26 PM, by Greg Laden

... 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:

cat mouse.txt

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."

Answer:

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.
7
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.

Share on Facebook
Share on StumbleUpon
Share on Facebook

TrackBacks

TrackBack URL for this entry: http://scienceblogs.com/mt/pings/116879

Comments

1

The cat command works great on my Mac. BSD Unix underneath, don't ya know. :-)

Posted by: Quiet Desperation | August 19, 2009 4:49 PM

2

Maybe it's just the difficulty reading the output on this page in the exact same font as the discussion, but I don't understand how "cat mouse.txt|grep mice|wc" returns "2 10 66" when "cat -n mouse.txt" shows four lines containing the word "mice" (1, 2, 8, 13).

Posted by: mk | August 19, 2009 5:44 PM

3

Never mind, I missed that lines 1 and 2 have "Mice" not "mice." And/or I was thinking grep -i.

Posted by: mk | August 19, 2009 5:46 PM

4

"type" is the win/dos equiv. which is stupid. But similar in principle. Pipe it to find and you basically have the same function as grep. Type doesn't have a numbering function, but find does and will give you the same output:

type mouse.txt | find /n /v ""

and...uh...I need perl. Because it's awesome.

Admittedly "type /?" does not have nearly the impact of "man cat".

Posted by: Spiv | August 20, 2009 10:04 AM

5

You neglected to mention one of the commands primary purposes, which is to concatenate files. For example,

cat file1 file2 file3 >bigfile

copies file1 to bigfile, then appends file2 to bigfile, and finally appends file3 to bigfile.

Posted by: LightningRose | August 23, 2009 12:56 AM

Post a Comment

(Email is required for authentication purposes only. On some blogs, comments are moderated for spam, so your comment may not appear immediately.)





ScienceBlogs

Search ScienceBlogs:

Go to:

Advertisement
Follow ScienceBlogs on Twitter

© 2006-2011 ScienceBlogs LLC. ScienceBlogs is a registered trademark of ScienceBlogs LLC. All rights reserved.