EN
Bash - how to count lines in a document?
9
points
To count lines, words or characters we can use wc
command:
$ cat /etc/passwd | wc
$ wc /etc/passwd
Examples:
1. Single file:
$ wc test.txt
5 6 16 test.txt
Where:
- 5 - number of rows
- 6 - number of words
- 16 - number of characters
2. Multiple files:
$ wc foo.txt bar.txt
40 149 947 foo.txt
2294 16638 97724 bar.txt
2334 16787 98671 total
There are few useful modifications too:
wc -l path/to/file
- prints only total number of lines (note that if the last line does not have \n, the line will be not counted)wc -c path/to/file
- prints only total number of byteswc -m path/to/file
- prints only total number of characterwc -L path/to/file
- prints only length of longest linewc -w path/to/file
- prints only number of words