Why does wc -l tell me that this non-empty file has 0 lines?

10,366

The issue you're experiencing is that wc -l counts new lines. Since you haven't in fact typed the \n there is in fact zero new lines.

excerpt from wc man page

Print newline, word, and byte counts for each FILE, and a total line if more than one FILE is specified. With no FILE, or when FILE is -, read standard input. A word is a non-zero-length sequence of characters delimited by white space.

If you switch it around so that wc counts characters (-c) you'll see that it is in fact working just fine and can count the number of characters:

$ xsel -o | wc -c
14

Saving it to a file has the effect of adding a newline at the end of the file.

Share:
10,366

Related videos on Youtube

Beta4
Author by

Beta4

Updated on September 18, 2022

Comments

  • Beta4
    Beta4 over 1 year

    xclip/xsel are utilities that allow you to pipe clipboard output to the shell on Ubuntu. Unfortunately, they seem to fail in combination with the wc command.

    Try this. Open a new file and type:

    this is a line
    

    without hitting the enter key at the end of the line.

    Now, copy this text and try the following:

    $ xsel -o | wc -l
    0
    

    The output is 0, when in fact it should be 1. Save the file (call it myfile.txt).

    Now try:

    $ cat myfile.txt | wc -l
    1
    

    The output is 1, as expected.

    Why is the output 0 in the first case?