[ale] Bash script question

Danny Cox danscox at mindspring.com
Wed May 14 20:37:52 EDT 2003


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





More information about the Ale mailing list