7

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?

2 Answers 2

8

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.

0

wc counts the end of line characters. The first time you just marked the text, not containing any end of line character. When you save the file, the editor appends an end of line character which is counted by wc.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .