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.

More like this

A repost, continuing along the lines of bashing the shell. Having examined Learning the bash Shell (In a Nutshell (O'Reilly)) (see here, here, and here), it is now time to turn to a more advanced reference to help you geek out on your Linux computer. If you want to have only one book on bash,…
ls | wc -l How many files are in the current directory on my hard drive? The command ls gives me a list of files (ls stands for "list stuff") the vertical line is a pipe. This means the standard output of the left side of the pipe is sent (like in a pipe) to the standard input of the right side…
I thought I was done with the command line for the week, but then I did something cool that I thought I'd share with you. Linux users only ... others will think this is silly ... join me below the fold. OK, are we alone? Good. It's nice to be away from all those Windows Symps for a while. Oh…
I've got a real treat for you pathological programming fans! Today, we're going to take a quick look at the worlds most *useful* pathological programming language: TECO. TECO is one of the most influential pieces of software ever written. If, by chance, you've ever heard of a little editor called "…

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.

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.

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

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.

"what's the count"?

Hmm. It used to be 'word count'.

Dude...
> wc email.txt

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.

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.