Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not empty file, but "wc -l" outputs 0

Tags:

file

bash

I have a non-empty file (even a big one, 400Ko), that I can read with less.

But if I try to output the number of lines with wc -l /path/to/file it outputs 0.

How can it be possible?

like image 872
Simon Avatar asked Nov 15 '25 11:11

Simon


2 Answers

You can verify for yourself that the file contains no newline/linefeed (ASCII 10) characters, which would result in wc -l reporting 0 lines.

  1. First, count the characters in your file:

    wc -c /path/to/file
    

    You should get a non-zero value.

  2. Now, filter out everything that isn't a newline:

    tr -dc '\n' /path/to/file | wc -c
    

    You should get back 0.

  3. Or, delete the newlines and count the result.

    tr -d '\n' | wc -c
    

    You should get back the same value as in step 1.

like image 78
chepner Avatar answered Nov 17 '25 08:11

chepner


wc counts number of '\n' characters in the file. Could it be that your file does not contain one?

Here is the GNU source: https://www.gnu.org/software/cflow/manual/html_node/Source-of-wc-command.html

look for COUNT(c) macro.

like image 44
vim_ Avatar answered Nov 17 '25 09:11

vim_