Now on ScienceBlogs: Open Lab: Time is Ticking!

Seed Media Group

Collective Imagination

Greg Laden's Blog

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

Recent Comments

Profile


Welcome to Greg Laden's Blog.




Nature Blog Network



Search

Join the best atheist themed blogroll!

Archives

Recent Posts

« Mississippi Burning | Main | In the beginning there was cat ... »

Linux One Liners

Posted on: August 4, 2008 8:00 PM, by Greg Laden

I have a file called "email.txt" and I want to count the number of lines in it.



cat email.txt | wc -l
cat email.txt | sed -n '$='

The first example uses wc (which stands for "what's the count") with the -l (lines only) optin. The contents is pushed via cat through standard output into a pipe and thusly becomes standard input for the next command (wc).

The second example does the same thing with sed, and demonstrates a number of interesting aspects of sed. the equal sign causes the output of the current line number. Sed normally runs through all the lines of the file or input given to it. So this one liner would print the line number for each line of email text. However, the -n option causes sed to repress automatic printing unless specifically requested.

Share this: Stumbleupon Reddit Email + More

TrackBacks

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

Comments

1

Since both sed and wc will take the file name as an argument, you've won the useless use of cat award twice in one post.

btw, your new layout looks hideous and is confusing in firefox.

Posted by: llewelly | August 4, 2008 8:45 PM

2

Dude, this page is friend. Something happened to the Seed wrapper. It's very web 1.0. This isn't a reference to llewlly's comment. Something, somewhere is very wrong in the code for this post, and only this post.

Posted by: Jeb, FCD | August 4, 2008 8:56 PM

3

Yes this page is borked in Safari also. Virtually unreadable. I do make out that someone has already pointed out your "useless use of cat" an honorable mistake that people have enjoyed pointing out for at least 20 years. (That I remember anyway.)

Posted by: Markk | August 4, 2008 8:59 PM

4

llewelly

The prejudiced against "cat" is very interesting. The fact that any command will take a file name as an argument, in and of itself, is usually used to make that case but it is relevant. Cat is an elegant way to begin a command that you want to test, then add to with pipes or by adding arguments to cat. (most people don't know that cat has some kick ass arguments) For this reason I generally do it, and scoff at those who parrot this recently (post-disco, anyway) trendy complaint. Ha!

Sorry about the borked layout. There was something in this post that did that. Odd. Whatever it was, if I can replicate it, I've probably got a new gateway into the dark side of the web. What you were seeing there is more or less what a Sb web page looks like when it becomes detached from its style sheet.

Posted by: greg laden | August 4, 2008 10:30 PM

5

"what's the count"?

Hmm. It used to be 'word count'.

Posted by: 6EQUJ5 | August 5, 2008 4:26 AM

6

Dude...
> wc email.txt

Posted by: toast | August 5, 2008 9:30 AM

7
The prejudiced against "cat" is very interesting.
Note the adjective 'useless' applies to the word 'use' , not to the word 'cat'.
(most people don't know that cat has some kick ass arguments)
If you had used one of cat's options, or were concatenating two or more files, I would not have objected.

Posted by: llewelly | August 5, 2008 10:06 AM

8
The second example does the same thing with sed ...
Actually - and I've no idea how I forgot to mention this yesterday - these two one-liners do not do the same thing. wc -l counts newline characters, whereas the sed command counts the logical ends of lines. Thus if you use them both on a file whose last line does not end with a newline, they will give different results.

Posted by: llewelly | August 5, 2008 10:15 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
Enter to win a free copy of The Monty Hall Problem
Visit the Collective Imagination blog
Advertisement
Collective Imagination

© 2006-2009 Seed Media Group LLC. ScienceBlogs is a registered trademark of Seed Media Group. All rights reserved.

Sites by Seed Media Group: Seed Media Group | ScienceBlogs | SEEDMAGAZINE.COM