Linux One Liners

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.


  1. #1 llewelly
    August 4, 2008

    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.

  2. #2 Jeb, FCD
    August 4, 2008

    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.

  3. #3 Markk
    August 4, 2008

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

  4. #4 greg laden
    August 4, 2008


    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.

  5. #5 6EQUJ5
    August 5, 2008

    “what’s the count”?

    Hmm. It used to be ‘word count’.

  6. #6 toast
    August 5, 2008

    > wc email.txt

  7. #7 llewelly
    August 5, 2008

    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.

  8. #8 llewelly
    August 5, 2008

    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.