wc: Is there anything it can’t count?!

Ostensibly, wc counts words. And for the purpose of wc, “a word is a non-zero-length sequence of characters delimited by white space.” Feed wc a file, and it will happily spit out a total for you.

2014-06-21-6m47421-wc-01

Except, as you can see, by default wc gives more than just word counts. It counts newlines, then words, then bytes. Perhaps wc should be renamed to “nwbc?”

But wait, there’s more: wc can also total up characters, which may come in handy from time to time.

2014-06-21-6m47421-wc-02

And as a curiosity, wc can show the length of the longest line in a file.

kmandla@6m47421: ~/temp$ wc -L output.txt 
16 output.txt

I can’t imagine a situation where that would be useful, but I don’t dare argue with a member of the hallowed coreutils package. 😀

So in short, wc will count newlines, words, bytes, characters and even show the length of the longest line in a file. Is there anything it can’t count?! 😯

5 thoughts on “wc: Is there anything it can’t count?!

  1. John

    I wish I had known about the -L flag a few days ago! My IDE was unable to open a particular file normally because it claimed it had lines exceeding maximum length. I was curious, so I went to the command line and did something stupid like:

    while read l ; do wc -c <<< "$l" ; done < sourcefile.cc | sort -n

    Whereas apparently the faster and simpler approach would've been

    wc -L sourcefile.cc

  2. Pingback: Links 25/6/2014: A Lot of Android News, Peppermint Five | Techrights

  3. Pingback: xargs: Obviously first | Inconsolation

  4. Pingback: cloc: Clock your code | Inconsolation

  5. Pingback: Tricks of the trade | Motho ke motho ka botho

Comments are closed.