Counting Lines, Words, and File Size


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"




UNIX. The Complete Reference
UNIX: The Complete Reference, Second Edition (Complete Reference Series)
ISBN: 0072263369
EAN: 2147483647
Year: 2006
Pages: 316

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net