How to count the lines of a file?
+3
−0
How to get the number of lines in a file?
I.e. for a file like this:
Line one
Line 2
Final line
I would like to do something like this:
$ count-lines /path/to/the/file/above
3
2 answers
+1
−0
One way is to use grep
:
$ grep --count ^ /path/to/the/file
The ^
character matches a start of a new line, so it basically counts the number of starting lines.
0 comment threads
+7
−0
I think the typical way to do this uses wc
("word count") with the -l
("lines") option.
$ wc -l /path/to/file
47 /path/to/file
$ wc -l </path/to/file
47
$ cat /path/to/file | wc -l
47
wc
with no options prints the number of lines, words, and bytes, but -l
can limit it to just lines. As a comment notes: "lines" is the number of newline characters, so you may be off-by-one if you don't have a trailing newline.
0 comment threads