LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-22-2017, 02:28 PM   #1
gursewaks056
LQ Newbie
 
Registered: Sep 2017
Location: Chandigarh
Distribution: Ubuntu 16.04 LTS
Posts: 13

Rep: Reputation: Disabled
wc -c is not working properly for Strings


When I count the number of character in a string using
`wc -c $Str` it always gives a one character extra to me.
e.g : str="Good" ; len=`echo $str | wc -c`
it results in len=5 not 4.
 
Old 09-22-2017, 02:35 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,231
Blog Entries: 24

Rep: Reputation: 4170Reputation: 4170Reputation: 4170Reputation: 4170Reputation: 4170Reputation: 4170Reputation: 4170Reputation: 4170Reputation: 4170Reputation: 4170Reputation: 4170
When you echo the string, echo appends a newline character which is counted by wc.

Suppress the newline to do this with echo, or better might be to use the shell's built in operator for getting the string length, ${#var}.
 
1 members found this post helpful.
Old 09-25-2017, 05:11 AM   #3
aragorn2101
Member
 
Registered: Dec 2012
Location: Mauritius
Distribution: Slackware
Posts: 567

Rep: Reputation: 301Reputation: 301Reputation: 301Reputation: 301
Quote:
Originally Posted by gursewaks056 View Post
When I count the number of character in a string using
`wc -c $Str` it always gives a one character extra to me.
e.g : str="Good" ; len=`echo $str | wc -c`
it results in len=5 not 4.
Yes, as astrogeek pointed out, echo adds a newline character at the end, so it increases the count. You can use the "-n" argument with echo in order to suppress the newline character.

But first, I would like to make you notice that wc with argument "-c" will count number of bytes, not number of characters. You are getting the correct number of characters (counting the newline) because every character is 1 byte each. But it will not work with multibyte characters like UTF-8.
Code:
echo -n "Good" | wc -c
4

echo -n "Fête" | wc -c
5

# Whereas, with the argument "-m"
echo -n "Good" | wc -m
4

echo -n "Fête" | wc -m
4
Please read the man pages for wc for more information.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Working with strings in an xml file C++ vbx_wx Programming 1 03-08-2011 06:42 AM
[SOLVED] Searching and replacing strings in a file with strings in other files xndd Linux - Newbie 16 07-29-2010 03:40 PM
how to find duplicate strings in vertical column of strings markhod Programming 7 11-02-2005 05:04 AM
guidlines on working with strings in C? zero79 Programming 1 02-22-2005 04:54 PM
Perl>>working with strings format_c Programming 2 11-15-2001 05:21 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07: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