Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

in tmp I type a single character,but wc -c shows 2,why?

share|improve this question

3 Answers 3

up vote 7 down vote accepted

Because newlines are characters too. Tell your text editor to not add one at the end of the file. No, I don't know how.

share|improve this answer
2  
as a workaround, you could count newlines with wc -l and substract them from the count of wc -c. If you are counting directly from a piped / redirected output, consider using tee for splitting it. –  the-wabbit Jul 6 '11 at 6:09

One way is to tr to delete newlines, then you can count the characters.

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
share|improve this answer

Sorry for the late answer. But I saw this question just now with no right reply.

Use echo -n Hello | wc -m.

This is faster than deleting the newline.

Sashidhar, Virginia

share|improve this answer
    
You're assuming that there is any sort of control over the output process. –  Ignacio Vazquez-Abrams Dec 12 '13 at 2:30
    
And this was already discussed in Emeraldo's answer from a year ago. –  Michael Hampton Dec 12 '13 at 2:33

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.