Languages
[Edit]
EN

Bash - how to count lines in a document?

9 points
Created by:
Marcin
15740

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 bytes
  • wc -m path/to/file - prints only total number of character 
  • wc -L path/to/file - prints only length of longest line 
  • wc -w path/to/file - prints only number of words 
Native Advertising
50 000 ad impressions - 449$
🚀
Get your tech brand or product in front of software developers.
For more information contact us:
Red dot
Dirask - friendly IT community for everyone.

❤️💻 🙂

Join