WC Team on Linux

Below is the content saved in my file

This is my Input

So, using the wc -c command, we can get the number of characters stored in the file.

My expected output for the file above that is edited using VIM in Ubuntu is 16. But the wc -c command returns 17.

Why is this so? At the end of the line there is not even a carriage return. So what is the 17th character?

+4
source share
4 answers

Of course you are logged in. perhaps you cannot see it. consider two examples:

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

because -n to avoid input, but

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

Look at this example, also look at a new line.

enter image description here

How to see a new line?

echo "This is my Input" | od -c

od dump
-c ASCII

od enter image description here

+4

Linux, VIM , , . Hex- VIM.

:!xxd hex-dump hexdump yourfile.

0000000: 5468 6973 2069 7320 6d79 2049 6e70 7574  This is my Input
0000010: 0a                                       .
~                                                                                                                                 
~                                                                                                                                 
~    

, 0a .

, wc -c, , 17, .

+1

17 - "/0" chaeracter.

+1

, , / , / . wc -c echo.

echo k | wc -c 

returns 2 because 1 for k and 1 for a new line added by echo

and

echo -n k | wc -c

returns 1 because -n suppresses the newline.

but wc -c always reads a new line.

You can try

printf k | wc -c 

returns 1

see what it does in the file

bash-4.1$ echo 1234 > newfile
bash-4.1$ cat newfile
1234
bash-4.1$ cat -e newfile
1234$
bash-4.1$ printf 1234 > newfile
bash-4.1$ cat newfile
1234bash-4.1$ cat -e newfile
1234bash-4.1$
0
source

Source: https://habr.com/ru/post/1599269/


All Articles