The wc command under Linux counts the number of lines/words/characters/characters in the longest line of a file

wc command help

$ wc --help
Usage: wc [OPTION]... [FILE]...
  or:  wc [OPTION]... --files0-from=F
Print newline, word, and byte counts for each FILE, and a total line if
more than one FILE is specified.  A word is a non-zero-length sequence of
characters delimited by white space.

With no FILE, or when FILE is -, read standard input.

The options below may be used to select which counts are printed, always in
the following order: newline, word, character, byte, maximum line length.
  -c, --bytes            print the byte counts
  -m, --chars            print the character counts
  -l, --lines            print the newline counts
      --files0-from=F    read input from the files specified by
                           NUL-terminated names in file F;
                           If F is - then read names from standard input
  -L, --max-line-length  print the maximum display width
  -w, --words            print the word counts
      --help     display this help and exit
      --version  output version information and exit

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
Full documentation at: <http://www.gnu.org/software/coreutils/wc>
or available locally via: info '(coreutils) wc invocation'

command usage

count rows

$ wc -l /usr/share/dict/american-english
99171 /usr/share/dict/american-english

count words

$ wc -w /usr/share/dict/american-english
99171 /usr/share/dict/american-english

count characters

wc -m /usr/share/dict/american-english
938587 /usr/share/dict/american-english

Statistics bytes

wc -c /usr/share/dict/american-english
938848 /usr/share/dict/american-english

Note that the difference between -c and -m is that for multi-byte characters, such as GBK, UTF-8 encoded Chinese, record one in -m, and record multiple in -c, such as the following test, the default encoding of ubuntu is UTF -8, Chinese is 3 bytes

$ echo -n " 123, test " | wc - c
 12 
$ echo -n " ​​123, test " | wc - m
 8

Count the longest line

$ wc -L /usr/share/dict/american-english
23 /usr/share/dict/american-english

If you only want to get numbers without printing file names, you can use the following two methods. From the perspective of saving memory, the former method is recommended

$ wc -l /usr/share/dict/american-english | awk '{print $1}'
99171
$ cat /usr/share/dict/american-english | wc -l
99171

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324593881&siteId=291194637