liveBook · Manning

Wc utility

As explained in Tiny Python Projects: Learn coding and testing with puzzles and games

wc is a Unix command-line utility that reports counts of bytes, characters, lines, and words in files or standard input.

wc reports lines, words, and bytes for one or more files, or reads equivalent input from standard input when no filenames are supplied. section 6.0

Its command-line behavior includes:

  • Formats each count in an eight-character-wide column followed by the filename. section 6.1.3
  • Adds a total row when multiple files are counted. section 6.0
  • Accepts flags for selected character, line, and word columns; splitting on spaces provides an approximate word count. section 6.1.3 section 6.4

Files and pipelines

wc fox.txt produces 1 line, 9 words, and 45 bytes. Multiple files produce individual rows and a total; piping grep output into wc counts the matching stream. section 6.1.3 section 6.0

$ ./wc.py ../inputs/const.txt ../inputs/sonnet-29.txt
    865    7620   44841 ../inputs/const.txt
     17     118     661 ../inputs/sonnet-29.txt
    882    7738   45502 total

Compare per-file counts with the aggregate total in a concrete terminal listing. section 6.0

How Tiny Python Projects: Learn coding and testing with puzzles and games covers it

How deep it goes
Explains counters, line iteration, splitting, formatting, and flags, but treats punctuation-aware word detection as later work.
Examples
Demonstrates fox.txt, multiple files, grep output, redirection, and a Python reimplementation named wc.py.
What it assumes
Teaches file handles, positional arguments, loops, strings, and standard streams within the implementation.
What you can do after
Build it from scratch in Python with file, STDIN, counting, formatting, and option-handling support.
How it connects
Connects standard input and output, pipes, redirection, grep, and positional arguments.

Also explained in

Compare how 2 books explain wc utility →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Tiny Python Projects: Learn coding and testing with puzzles and games. The section links point to the parts of the book it draws on.