[ale] Bash script question

Jim Popovitch jimpop at yahoo.com
Wed May 14 20:58:07 EDT 2003


One little fix for Danny's #1 option, you will need a 'g' at the end of the sed
statement to make it work.  Something like this will work:

echo "Field1,Field2,Field3," | sed -e 's/[^,]//g' | awk '{print length}'

-Jim P.

> -----Original Message-----
> From: Danny Cox
> Sent: Wednesday, May 14, 2003 8:38 PM
> To: ALE Mailing List
> Subject: Re: [ale] Bash script question
>
>
> Bob,
>
> On Wed, 2003-05-14 at 20:00, Bob Kruger wrote:
> > Is there a utility that I can run from bash that I can use to determine
> > the number of times a certain character appears in a line?
> >
> > Example - I want to determine the number of times a comma (,) appears in
> > the line:
> >
> > Field1,Field2,Field3,
> >
> > Yes, it can be done from Perl, but I would like to do this from bash.
>
> 	Well, bash itself can't do it, but there are several ways to link up
> commands using pipes:
>
> 	1) sed 's/[^,]//' file | awk '{print length}'
> 	2) awk '-F,' '{print NF-1}' file
> 	3) tr -cd ',\n' file | awk '{print length}'
>
> you could also use just awk again, with a combination of gensub and
> print length, using the #1 regex above, but I'm too lazy right now to
> code it up ;-).  Besides, it would look like line noise (like the above
> don't ;-).
>
> 	Hope this helps!
>
> --
> kernel, n.: A part of an operating system that preserves the
> medieval traditions of sorcery and black art.
>
> Danny
>
> _______________________________________________
> Ale mailing list
> Ale at ale.org
> http://www.ale.org/mailman/listinfo/ale
>

_______________________________________________
Ale mailing list
Ale at ale.org
http://www.ale.org/mailman/listinfo/ale





More information about the Ale mailing list