I have lines like these, and I want to know how many lines I actually have...
09:16:39 AM all 2.00 0.00 4.00 0.00 0.00 0.00 0.00 0.0
I've been using this:
cat myfile.txt | wc -l
I prefer it over the accepted answer because it does not print the filename, and you don't have to use awk
to fix that. Accepted answer:
wc -l myfile.txt
But I think the best one is GGB667's answer:
wc -l < myfile.txt
I will probably be using that from now on. It's slightly shorter than my way. I am putting up my old way of doing it in case anyone prefers it. The output is the same with those two methods.
If all you want is the number of lines (and not the number of lines and the stupid file name coming back):
wc -l < /filepath/filename.ext
As previously mentioned these also work (but are inferior for other reasons):
awk 'END{print NR}' file # not on all unixes
sed -n '$=' file # (GNU sed) also not on all unixes
grep -c ".*" file # overkill and probably also slower
wc -l
does not count lines.Yes, this answer may be a bit late to the party, but I haven't found anyone document a more robust solution in the answers yet.
Contrary to popular belief, POSIX does not require files to end with a newline character at all. Yes, the definition of a POSIX 3.206 Line is as follows:
A sequence of zero or more non- <newline> characters plus a terminating character.
However, what many people are not aware of is that POSIX also defines POSIX 3.195 Incomplete Line as:
A sequence of one or more non- <newline> characters at the end of the file.
Hence, files without a trailing LF
are perfectly POSIX-compliant.
If you choose not to support both EOF types, your program is not POSIX-compliant.
As an example, let's have look at the following file.
1 This is the first line.
2 This is the second line.
No matter the EOF, I'm sure you would agree that there are two lines. You figured that out by looking at how many lines have been started, not by looking at how many lines have been terminated. In other words, as per POSIX, these two files both have the same amount of lines:
1 This is the first line.\n
2 This is the second line.\n
1 This is the first line.\n
2 This is the second line.
The man page is relatively clear about wc
counting newlines, with a newline just being a 0x0a
character:
NAME
wc - print newline, word, and byte counts for each file
Hence, wc
doesn't even attempt to count what you might call a "line". Using wc
to count lines can very well lead to miscounts, depending on the EOF of your input file.
You can use grep
to count lines just as in the example above. This solution is both more robust and precise, and it supports all the different flavors of what a line in your file could be:
$ grep -c ^ FILE
The tool wc
is the "word counter" in UNIX and UNIX-like operating systems, but you can also use it to count lines in a file by adding the -l
option.
wc -l foo
will count the number of lines in foo
. You can also pipe output from a program like this: ls -l | wc -l
, which will tell you how many files are in the current directory (plus one).
If you want to check the total line of all the files in a directory ,you can use find and wc:
find . -type f -exec wc -l {} +
cat file.log | wc -l | grep -oE '\d+'
grep -oE '\d+'
: In order to return the digit numbers ONLY.