- Linux Shell Scripting Cookbook(Third Edition)
- Clif Flynt Sarath Lakshman Shantanu Tushar
- 146字
- 2021-07-09 19:46:37
How to do it...
The wc command supports options to count the number of lines, words, and characters:
- Count the number of lines:
$ wc -l file
- To use stdin as input, use this command:
$ cat file | wc -l
- Count the number of words:
$ wc -w file $ cat file | wc -w
- Count the number of characters:
$ wc -c file $ cat file | wc -c
To count the characters in a text string, use this command:
echo -n 1234 | wc -c 4
Here, -n deletes the final newline character.
- To print the number of lines, words, and characters, execute wc without any options:
$ wc file 1435 15763 112200
Those are the number of lines, words, and characters.
- Print the length of the longest line in a file with the -L option:
$ wc file -L 205