Unix Shell - Character Count

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > February 2005 > Character Count





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Character Count
mshetty@mail.com

2005-02-22, 5: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

Icarus Sparry

2005-02-22, 5: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

Chris F.A. Johnson

2005-02-22, 5: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
Ed Morton

2005-02-22, 5: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.
Chris F.A. Johnson

2005-02-22, 5: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
Geoff Clare

2005-02-25, 5: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>

Bill Seivert

2005-02-26, 2: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

Rakesh Sharma

2005-02-28, 8:01 am


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 $#

Rakesh Sharma

2005-02-28, 8:01 am


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

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com