Why Does the Wc Command Count One More Character Than Expected

Why does wc count one extra character in my file?

You have a trailing new line and this is what wc reports.

See for example if we create a file with printf:

$ printf "hello" > a
$ cat a | hexdump -c
0000000 h e l l o
0000005
$ wc a
0 1 5 a

However, if we write with something like echo, a trailing new line is appended:

$ echo "hello" > a
$ cat a | hexdump -c
0000000 h e l l o \n
0000006
$ wc a
1 1 6 a

wc -c counting wrong number of characters unix

Since you're interested in counting the number of columns, you could use awk for this:

Using your input:

$ cat file
A,0,0,0,21,36,12,0,0,0,17.2,34,18,17.2,30.5,96,126,517,2399,2,111.83,38.583,111,1,0,0,0,0,0,0

gives:

$ awk -F, '{print NF}' file
30

If you're interested in the number of comma's:

$ head -1 file | awk -F, '{print NF-1}'
29

BTW, I think you trying to call wc -m to count the characters.

Can someone explain why the outcome isn’t the same?

echo produces a new-line which is counted as an additional character, as @khelwood comments:

$ echo "ensPpaJxUanRSxRzWSqMcLrYZDhkCp" | python -c 'import sys; print(list(sys.stdin))' 
['ensPpaJxUanRSxRzWSqMcLrYZDhkCp\n']

WC command of mac showing one less result

Last line does not contain a new line.

One trick to get the result you want would be:

sed -n '=' <yourfile> | wc -l

This tells sed just to print the line number of each line in your file which wc then counts. There are probably better solutions, but this works.

Using the line count command wc -l document_name (Linux), I am getting a return of the number of lines minus 1

Be careful with wc as it will NOT count the very last line unless it has the EOL character at the end. Use grep -c "" filename instead.

Why does the file created using a text editor contain one byte more than expected?

There's reasoning in favor of having all lines terminated with a newline character:

  • Why should text files end with a newline?

And there are text editors that are set up to add a trailing newline character automatically (if not already there):

  • How to stop Gedit, Gvim, Vim, Nano from adding End-of-File newline char?

Probably that is why you observe the unexpected file size.

To inspect the actual content of such files, I like to use hexdump:

$ hexdump -C test
00000000 61 20 76 0a 62 62 0a 65 0a |a v.bb.e.|
00000009

Counting all characters except space in the using the ls|wc-c?

You could delete all spaces:

ls | tr -d ' ' | wc -c


Related Topics



Leave a reply



Submit