Character Count
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Shell > Character Count




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Character Count  
mshetty@mail.com


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-22-05 10:52 PM

Hi,

How do I find the count of a specific character in a line?

For eg: If my line is "|a |a |a |b |c"

then looking for "a" should return 3.

Is there a way to directly obtain this?

Thanks and Regards,
M Shetty






[ Post a follow-up to this message ]



    Re: Character Count  
Icarus Sparry


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-22-05 10:52 PM

On Tue, 22 Feb 2005 07:03:51 -0800, mshetty wrote:

> Hi,
>
> How do I find the count of a specific character in a line?
>
> For eg: If my line is "|a |a |a |b |c"
>
> then looking for "a" should return 3.
>
> Is there a way to directly obtain this?

For a single character, use 'tr' to remove everything else, and then use
either wc or awk to count what is left.

echo "|a |a |a |b |c" | tr -c -d a | wc -c






[ Post a follow-up to this message ]



    Re: Character Count  
Chris F.A. Johnson


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-22-05 10:53 PM

On Tue, 22 Feb 2005 at 15:03 GMT, mshetty@mail.com wrote:
> Hi,
>
> How do I find the count of a specific character in a line?
>
> For eg: If my line is "|a |a |a |b |c"
>
> then looking for "a" should return 3.
>
> Is there a way to directly obtain this?

With bash or ksh93:

line="|a |a |a |b |c"
x=${line//[^a]/}
echo ${#x}


With any Bourne-type shell:

set -- `tr -c a ' ' <<.
$line
.
`
echo $#


--
Chris F.A. Johnson                  http://cfaj.freeshell.org/shell
 ========================================
===========================
My code (if any) in this post is copyright 2005, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License





[ Post a follow-up to this message ]



    Re: Character Count  
Ed Morton


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-22-05 10:53 PM



Icarus Sparry wrote:
> On Tue, 22 Feb 2005 07:03:51 -0800, mshetty wrote:
>
> 
>
>
> For a single character, use 'tr' to remove everything else, and then use
> either wc or awk to count what is left.
>
> echo "|a |a |a |b |c" | tr -c -d a | wc -c
>

Or only use awk:

PS1> echo "|a |a |a |b |c" | awk -Fa '{print NF-1}'
3

Regards,

Ed.





[ Post a follow-up to this message ]



    Re: Character Count  
Chris F.A. Johnson


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-22-05 10:53 PM

On Tue, 22 Feb 2005 at 15:41 GMT, Ed Morton wrote:
>
>
> Icarus Sparry wrote: 
>
> Or only use awk:
>
> PS1> echo "|a |a |a |b |c" | awk -Fa '{print NF-1}'
> 3

Or, without any external command:

(
line="|a |a |a |b |c"
IFS=a
set -- $line
shift
echo $#
)

--
Chris F.A. Johnson                  http://cfaj.freeshell.org/shell
 ========================================
===========================
My code (if any) in this post is copyright 2005, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License





[ Post a follow-up to this message ]



    Re: Character Count  
Geoff Clare


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-25-05 10:58 PM

"Chris F.A. Johnson" <cfajohnson@gmail.com> wrote, on Tue, 22 Feb 2005:

> (
>  line="|a |a |a |b |c"
>  IFS=a
>  set -- $line
>  shift
>  echo $#
> )

Note that some shells (including ksh and bash) will give the
wrong answer if there is an "a" at the end of the line:

$ ( line="|a |a |a |b |c"; IFS=a; set -- $line; shift; echo $# )
3

$ ( line="|a |a |c |b |a"; IFS=a; set -- $line; shift; echo $# )
2

There really ought to be a "set -f" in there as well, to prevent
filename globbing.

--
Geoff Clare <netnews@gclare.org.uk>






[ Post a follow-up to this message ]



    Re: Character Count  
Bill Seivert


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-26-05 07:50 AM



Icarus Sparry wrote:
> On Tue, 22 Feb 2005 07:03:51 -0800, mshetty wrote:
>
> 
>
>
> For a single character, use 'tr' to remove everything else, and then use
> either wc or awk to count what is left.
>
> echo "|a |a |a |b |c" | tr -c -d a | wc -c
>

Or use sed to remove everything else:

echo "|a |a |a |b |c" | sed -e "s/[^a]//g" | wc -c

Bill Seivert






[ Post a follow-up to this message ]



    Re: Character Count  
Rakesh Sharma


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-28-05 01:01 PM


mshetty@mail.com wrote:
>
> How do I find the count of a specific character in a line?
>
> For eg: If my line is "|a |a |a |b |c"
>
> then looking for "a" should return 3.
>

#!/bin/sh -u
line="|a |a |a |b |c"
set x `(printf '%s\n' "$line" | tr -cd 'a';echo) |\
sed -e 's/a/& /g'`
shift
echo $#






[ Post a follow-up to this message ]



    Re: Character Count  
Rakesh Sharma


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-28-05 01:01 PM


mshetty@mail.com wrote:
>
> How do I find the count of a specific character in a line?
>
> For eg: If my line is "|a |a |a |b |c"
>
> then looking for "a" should return 3.
>
> Is there a way to directly obtain this?
>

If there are consecutive 'a's in the variable line
then some bourne shells might not give consistent results,
because they don't treat word splitting specially if IFS
has characters other than <SPC><TAB><NL>.

A portable way to do it would be:

#!/bin/sh -u
line="aa|a |aa |b |c |aaaaa"
var=`printf '%s\n' "$line" | tr -cd 'a'`
expr "X$var" : '.*' - 1






[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 09:25 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register