UNIX: The Complete Reference, Second Edition (Complete Reference Series)

The command wc (word count) is a flexible little tool that provides several ways to count the size of a file. The command nl is another small tool. It can be used to add line numbers to a file.

WC

The command wc (word count) prints the number of bytes, lines, or words in a file. For example,

$ cat samplefile This file contains 143 bytes. It has 30 words, and it is 5 lines long. It has 3 lines that contain the number 3. The longest line is 41 bytes. $ wc −c samplefile # Size of the file in bytes. 143 samplefile $ wc −w samplefile # Number of words in the file. 30 samplefile $ grep 3 samplefile | wc −1 # Number of lines in file that contain "3". 3 $ wc −L samplefile # Length of the longest line. 41 samplefile

nl

To number each line in a file, use the command

$ nl filename > numbered

This will only add numbers at the beginning of nonempty lines. To number all the lines in a file, use

$ nl −ba hello.py 1 #!/usr/bin/python 2 3 print "Hello, world"

Категории