If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > count the number of characters in a string?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-12-04, 14:04
Racer_X Racer_X is offline
Registered User
 
Join Date: Nov 2003
Posts: 2
count the number of characters in a string?

I have a string ie. x="123 44 55 ffffccc xxx"
how do I go about counting just the characters, not the whitespace?

Thanks.
Reply With Quote
  #2 (permalink)  
Old 01-13-04, 03:24
chillies chillies is offline
Registered User
 
Join Date: Jul 2003
Location: Edinburgh
Posts: 35
Re: count the number of characters in a string?

Quote:
Originally posted by Racer_X
I have a string ie. x="123 44 55 ffffccc xxx"
how do I go about counting just the characters, not the whitespace?

Thanks.
Use a pipeline.

Use tr to remove the whitespace

use wc to count the remaining characters
Reply With Quote
  #3 (permalink)  
Old 01-13-04, 14:53
fla5do fla5do is offline
Registered User
 
Join Date: Oct 2003
Location: Germany
Posts: 138
I do it like this :
the char "@" does not occur in your string. Othewise use in FS another char which surely never can occur in your string.
##################### count the chars in string
x="123 44 55 ffffccc xxx"
chars=`echo $x | awk ' BEGIN { FS="@"}
{
x=length($1)
y=0
while ( y != x)
{
y=y+1
if (substr($1, y, 1 ) != " " )
{
chars=chars+1
}
}
} END {
print chars
}'`
echo "numbers of chars in variabe x "$chars
__________________
Greetings from germany
Peter F.

Last edited by fla5do; 01-13-04 at 15:57.
Reply With Quote
  #4 (permalink)  
Old 01-14-04, 03:01
chillies chillies is offline
Registered User
 
Join Date: Jul 2003
Location: Edinburgh
Posts: 35
Well, it looks like one cannot use /usr/bin/tr to delete characters. Also, echo appends a newline by default. So my updated version is:

echo -n $x | sed 's/ //g' | wc -c
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On