Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
cat > file
Amy looked at her watch. He was late. The sun was setting but Jake didn’t care.

wc file
1      16      82 file

Can somebody explain why wc command returns 3 extra characters in this case?

share|improve this question
    
I don't know.Typing that sentence, followed by a NL & Ctrl-D for me renders wc file 1 16 80 file What environment are you in? – tink Feb 23 at 22:12
5  
Because you have 3 spaces at the end of the line? Do od -c file to see exactly what is in there. – vonbrand Feb 23 at 23:58
    
Use wc -m to count characters. wc and wc -c give a byte count, not a character count. – Stéphane Chazelas Feb 24 at 10:42

wc shows 3 characters more because your example file contains a fancy Unicode apostrophe (most likely because you copied the contents from a browser or text editor):

$ cat file
Amy looked at her watch. He was late. The sun was setting but Jake didn’t care.
$ wc file
1      16      82 file

With plain ASCII apostrophe ':

$ cat file2
Amy looked at her watch. He was late. The sun was setting but Jake didn't care.
$ wc file
1      16      80 file2

wc by default displays the number of bytes per manual :

newline, word, and byte counts for each file

for character count an -m argument can be used:

$ cat file
Amy looked at her watch. He was late. The sun was setting but Jake didn’t care.
$ wc -m file
      80 file.txt
share|improve this answer
7  
Yes, and because wc is counting bytes, not characters. pubs.opengroup.org/onlinepubs/009604499/utilities/wc.html – Miles Feb 24 at 5:49
5  
Use wc -m to count characters, wc -c and the 3rd column in wc output count bytes, not characters. – Stéphane Chazelas Feb 24 at 10:43

pipe the file thrught xxd to see a hex output side-by-side of the ascii, this will let you see if there are extra characters which you can't see or are unprintable.

$ cat file
one‏ and ‏two

$ cat file | wc
      1       3      18

$ cat file | xxd
00000000: 6f6e 65e2 808f 2061 6e64 20e2 808f 7477  one... and ...tw
00000010: 6f0a                                     o.
share|improve this answer

wc counts bytes, not characters. If you want to count characters, you should use -m option:

cat > file
Amy looked at her watch. He was late. The sun was setting but Jake didn’t care.

wc -l -w -m file
1      16      80 file

The remaining "extra character" is indeed the newline you have at the end of the file.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.