LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-11-2010, 01:57 AM   #1
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Rep: Reputation: 107Reputation: 107
Question Character count of a word is coming wrong.


Hi All,

I am using RHEL 4.5.

I am trying to count no. of characters in a word but it is coming one more than what it actually should be.

Code:
[linux1@HMLINUX1 tmp]$ a=vikas
[linux1@HMLINUX1 tmp]$ echo $a | wc -c (byte count)
6
[linux1@HMLINUX1 tmp]$ echo $a | wc -m (char count)
6
I can have a work around by subtracting 1 from the output (6-1=5 in this case).
BUT, I am just curious to know, why the character count is coming as 6 and not 5.

Last edited by vikas027; 06-11-2010 at 02:00 AM. Reason: added something
 
Old 06-11-2010, 02:00 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

I do think the answer (6) is correct. -c is the byte count, not the character count, which is -m.

Hope this helps.
 
Old 06-11-2010, 02:01 AM   #3
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Original Poster
Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by druuna View Post
Hi,

I do think the answer (6) is correct. -c is the byte count, not the character count, which is -m.

Hope this helps.
I guess when you were typing this, I was editing my original post.

Even with "-m", it is giving same thing.
 
Old 06-11-2010, 02:16 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

And that (6 as answer) is correct as well (still early, not really awake yet ).

There's a newline at the end of the string which I do believe is counted by wc (be it -m or -c).
 
Old 06-11-2010, 02:26 AM   #5
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Original Poster
Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by druuna View Post
Hi,

And that (6 as answer) is correct as well (still early, not really awake yet ).

There's a newline at the end of the string which I do believe is counted by wc (be it -m or -c).

Might be, I also thing hitting an "Enter" may also be counted as a new character.

So, I am left with only option to subtract 1 from the output.
 
Old 06-11-2010, 02:30 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

I'm not able to test it myself atm, but maybe this will work:

wc -m < vitas
or
wc -m < $a

Hope this helps.
 
Old 06-11-2010, 02:31 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Indeed the newline character counts for one, as the od command clearly shows:
Code:
$ echo $a | od -c
0000000   v   i   k   a   s  \n
0000006
You might either avoid the newline using option -n of echo, or - better - compute the string length using parameter substitution:
Code:
$ echo -n $a | wc -m
5
$ echo ${#a}
5
 
1 members found this post helpful.
Old 06-11-2010, 03:32 AM   #8
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Original Poster
Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by colucix View Post
Indeed the newline character counts for one, as the od command clearly shows:
Code:
$ echo $a | od -c
0000000   v   i   k   a   s  \n
0000006
You might either avoid the newline using option -n of echo, or - better - compute the string length using parameter substitution:
Code:
$ echo -n $a | wc -m
5
$ echo ${#a}
5
Great !

I dont have other words.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help in word count command grunge_1 Linux - General 4 03-20-2009 04:01 AM
rsync uses wrong directory, has wrong size but right file count? brianpbarnes Linux - Software 1 02-23-2009 05:48 PM
variable length string using GD (word wrap, carriage return, word/character count)? frieza Programming 1 02-14-2009 05:21 PM
word count issue George2 Programming 6 11-27-2007 06:11 AM
word count pantera Programming 2 08-31-2004 07:23 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration