Operating System - HP-UX
1826214 Members
3135 Online
109691 Solutions
New Discussion

Re: wc -m results in shell script??

 
SOLVED
Go to solution
Ryan B
Frequent Advisor

wc -m results in shell script??

Why does "wc -m" return the following when counting characters of the input:

VAR1=TEST
echo $VAR1 | wc -m

The echo piped to wc returns a value of 5 vs 4. I was looking for 4 as a returned value because "TEST" is only 4 characters.

Thanks for your time.

Ryan
10 REPLIES 10
John Dvorchak
Honored Contributor
Solution

Re: wc -m results in shell script??

you may be forgetting about the N/L that echo puts after it echo'ed something. If you just put 4 characters in a test file and then do the wc -m you will get an answer of the expected 4.

From man echo:

echo(1) echo(1)

NAME
echo - echo (print) arguments

SYNOPSIS
echo [arg] ...

DESCRIPTION
echo writes its arguments separated by blanks and terminated by a
new-line on the standard output. It also understands C-like escape
conventions; beware of conflicts with the shell's use of \:
If it has wheels or a skirt, you can't afford it.
Sridhar Bhaskarla
Honored Contributor

Re: wc -m results in shell script??

Hi Ryan,

John was right on it. It's with echo. Do this

VAR1=TEST
printf "$VAR1" |wc -m

And see it for yourself.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Steven E. Protter
Exalted Contributor

Re: wc -m results in shell script??

Its probably counting the carriage return/linefeed at the end of TEST

You an adjust your tests if you wish.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Ryan B
Frequent Advisor

Re: wc -m results in shell script??

John~

Maybe I did something incorrect, but I created a test file and inserted the word "TEST" in the file with vi and saved the file. When I did the "wc -m testfile", I still get 5. The file was called "aaa" and contained the word "TEST". Here is the output from the wc command mentioned above:

5 aaa
Ryan B
Frequent Advisor

Re: wc -m results in shell script??

I will test the other ideas as well...

Thanks,

Ryan
Sridhar Bhaskarla
Honored Contributor

Re: wc -m results in shell script??

Hi,

Same thing. It's the extra "carriage return" character that is causing your problem.

printf "TEST" > file
wc -m file
cat file (you shouldn't get a new line here)

vi file

wc -m file
cat file (you will be put to new line)

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Ryan B
Frequent Advisor

Re: wc -m results in shell script??

Sri~

I briefly looked at the man page and it looks like printf does not return escape characters, etc and I am assuming that is why it only shows the result I am expecting. Can you just verify that for me?

Looks like I need to get familiar with printf...

Thanks for your time.
John Dvorchak
Honored Contributor

Re: wc -m results in shell script??

Ok Ryan you got bit with the vi command putting the N/L at the end just like the echo command. Try this

printf "TEST" > aaa
wc -m aaa
cat aaa |wc -m

They should now both be 4

Good luck




If it has wheels or a skirt, you can't afford it.
Sridhar Bhaskarla
Honored Contributor

Re: wc -m results in shell script??

Ryan,

That's only to show the example of how extra characters can get in. If you are using printf, you will need to be bit careful as depending on the type of the data you print and it's conversion format, the output may change. It doesn't put a new-line at the end of it's arguments unlike echo.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
A. Clay Stephenson
Acclaimed Contributor

Re: wc -m results in shell script??

Actually, you dont need printf, echo will do what you want, simply add a "\c" to not emit a LF.

VAR1=TEST
echo "${VAR1}\c" | wc -m
If it ain't broke, I can fix that.