Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wc -l is NOT counting last of the file if it does not have end of line character

I need to count all lines of an unix file. The file has 3 lines but wc -l gives only 2 count.

I understand that it is not counting last line because it does not have end of line character

Could any one please tell me how to count that line as well ?

like image 965
logan Avatar asked Jan 20 '15 05:01

logan


People also ask

Which option is used with wc for counting the number of characters in a file only?

5. Which option is used for counting the number of characters in a file only. Explanation: -c option when used with wc command display only the number of characters in the specified file.

How do you count lines in wc?

wc. The wc command is used to find the number of lines, characters, words, and bytes of a file. To find the number of lines using wc, we add the -l option. This will give us the total number of lines and the name of the file.

Which option will be used for counting lines with wc command?

The following are the options and usage provided by the command. wc -l : Prints the number of lines in a file. wc -w : prints the number of words in a file. wc -c : Displays the count of bytes in a file.

What is the meaning of the output of the command last wc?

The Command WC (word count) in Linux OS allows to find out the word count, newline count, and the count of bytes or characters in a file that is mentioned by the file arguments. The output that is returned from word count command will give you the count of lines in a file or the number of words or character in a file.


1 Answers

grep -c returns the number of matching lines. Just use an empty string "" as your matching expression:

$ echo -n $'a\nb\nc' > 2or3.txt
$ cat 2or3.txt | wc -l
2
$ grep -c "" 2or3.txt
3
like image 195
Stephan Brumme Avatar answered Oct 01 '22 22:10

Stephan Brumme