Search code examples
linuxwc

wc -m in unix adds one character


Counting a line with 4 characters with no new line character:

ACTG

wc -m gives me 5. With echo, I can fix this problem so

echo -n 'ACTG' | wc -m

But if ACTG is in a text file with no new line character, I get 5. Why is that so?

$ ls -l file

-rw-rw-r-- 1 user user 5 Feb 11 15:27 file
$ hexdump -C file

00000000  41 42 43 44 0a                                    |ABCD.|
00000005

Solution

  • As hexdump has shown you, whatever editor you are using is adding a '\n' or 0x0A (new line) character at the end of the line when you save the file, even if you aren't writing one explicitly.

    See: http://www.asciitable.com/