WC mac command shows one result

I have a text file larger than 60 MB. It has entries in lines 5105043, but when I do wc -l, it only gives 5105042 results, which is one less than the actual. Does anyone know why this is happening?

Is this a common thing when the file size is large?

+4
unix
Sep 27 '12 at 7:15
source share
2 answers

The last line does not contain a new line.

One trick to get the desired result would be:

sed -n '=' <yourfile> | wc -l 

This tells sed simply print the line number of each line in your file, which then counts wc . There are probably better solutions, but it works.

+6
Sep 27 '12 at 7:32
source share

The last line of your file is probably missing the end of a new line. IIRC, wc -l just counts the number of newlines in a file.

If you try: cat -A file.txt | tail cat -A file.txt | tail , does your last line contain the terminating dollar sign ( $ )?

EDIT:

Assuming there is no newline in the last line of your file, you can add a newline to correct it as follows:

 printf "\n" >> file.txt 

The results of wc -l should now be consistent.

+1
Sep 27 '12 at 7:22
source share



All Articles