Why does the wc command count one more character than expected?

Asked
Active3 hr before
Viewed126 times

8 Answers

expected
90%

So when you use wc -c to get the number of this file, it will return 17 that includes the new line symbol., You have a couple of typos here /0 instead of \0 and chaeracter versus character. The extra character being counted by wc -c isn't a null (0) it's a line terminator, likely a \n. In a DOS formatted file you'd get 18 because it would be a two byte line terminator \r\n. – Alain O'Dea Dec 2 '15 at 15:00 ,So, using wc -c command we can get the number of characters stored in the file.,Why is the output like this? There isn't even a carriage return at end of line. So, what is the 17th character?

Of course you had enter. Maybe you can't see it. Consider these two examples:

echo - n "This is my Input" | wc - c
16

Because -n is for avoiding enter, but

echo "This is my Input" | wc - c
17

How to see newline?

echo "This is my Input" | od - c
load more v
88%

Server Fault help chat ,Thanks for contributing an answer to Server Fault!,One way is to tr to delete newlines, then you can count the characters.,in tmp I type a single character,but wc -c shows 2,why?

Standard behavior:

echo HELLO | wc - m
# result: 6
echo - n HELLO | wc - m
# result: 5

To show the count of newline characters found:

echo HELLO | wc - l
# result: 1
echo - n HELLO | wc - l
# result: 0

Strip the newline character and count characters:

echo HELLO | tr - d '\n' | wc - m
# result: 5

Strip the newline character (and possible returns with \r) and count characters for an input file:

tr - d '\n\r' < input.txt | wc - m
load more v
72%

Thanks for contributing an answer to Unix & Linux Stack Exchange!,‘wc’ counts the number of bytes, characters, whitespace-separated words, and newlines in each given FILE, or standard input if none are given or for a FILE of ‘-’.,Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.,so if there is no final newline character in the file the "lines" part of the wc output will be one less than expected. For example the following will output 1

so if there is no final newline character in the file the "lines" part of the wc output will be one less than expected. For example the following will output 1

printf 'hello\nworld' | wc - l

The OP has confirmed in comments that vim is reporting the lack of the final newline. A simple fix if all the files are known to have this problem is

 for f in *
    do
       echo >> "$f"
    done

A way to add a newline conditionally to the end of all the files if they are missing one is to use sed.

sed - s - i '$s/$/\n/;P;d' *
load more v
65%

The following command will tell you how many more words are in new.file than in old.file: ,or you can count the number of files in a directory: ,Many shells have built-in arithmetic commands and don't really need expr ; however, expr works in all shells. ,The wc (word count) command counts the number of lines, words, and characters in the files you specify. (Like most Unix utilities, wc reads from its standard input if you don't specify a filename.) For example, the file letter has 120 lines, 734 words, and 4,297 characters:

% wc letter
120 734 4297 letter
load more v
75%

The wc always returns the count at the beginning of the file. If you were to pass in more than one file, you will be greeted by a third line with the total:,If you’re feeling especially frisky, you can also pass in a directory or wild card character, *, to see the counts for a whole bunch of files!,When you omit the arguments, wc will by default return the line, word and character count (without any heading or labels), the same as if you used the argument -lwm:,Note that we link to certain 3rd party products via affiliate or sponsored links. You can learn more about it here.

$ wc - w filename.txt
313 filename.txt
load more v
40%

We can then send that output through another pipe, to head, so that the full pipeline becomes:,wc is the ‘word count’ command: it counts the number of lines, words, and characters in files (from left to right, in that order).,wc counts lines, words, and characters in its inputs.,wc counts lines, words, and characters in its inputs.

$ ls molecules
load more v
22%

Could not perform requested text conversion.,The text file is untagged and you do not want to tag it or enable automatic conversion, and,A word is considered to be a character or characters delimited by white space.,When specifying values for filecodeset, use the values that Unicode Service supports.

wc myTextFile
load more v
60%

The wc program counts 3 lines, 5 words, and 21 characters, which are the expected counts.,The "get" function gets or reads one character from the file and stores it in the variable c (the highlighted code),In this way, every character is counted but words are only counted when the first character of the word is read.,reading any of the three white-space characters signals that the program is between words - that is, it's not in a word - and the flag is set to false

See the\ n
quick red\ n
fox
load more v

Other "expected-undefined" queries related to "Why does the wc command count one more character than expected?"