Counting position of a character

Hi All,

I have a file of the format :

idsfskjvfdznvdfjvh
ierwjfkncmvlkmc
xszkmdvnosndzjndf
weuhrndzierfncv
rndsjnsllshens
iernzkfndslkdhf
zkinewfinfvlkmvd

I wish to count the occurrences of character 'z' in the file.
I also need to find out the position of 'z' in various lines.
and finally replace 'z' if it occurs more than once in a line.

Thanks in advance
Regards
Rochit

requirement #1 - count z's

#!/bin/ksh
zees=$( tr -dc 'z' < inputfile)
echo ${#zees}

Hi Jim,

Thanks for replying. but your script only counts the total number of occurrences of 'z'. But the main prob. i.e its position in file is not found.

Regards
Rochit

Something like this (change NEW to whatever you want):

awk 'NF>1{
	printf "rec: %d ",FNR
	for (i=1;i<=NF-1; i++)
		printf ( index($0,$(i+1)) - 1 ) ? "pos: "index($0,$(i+1)) - 1" ": "pos: "length" "
	print ""
	tot += ( NF - 1 )
} 
NF > 2 {
	gsub(FS,"NEW")
	} 
{ 
	all[FNR] = $0
} END {
	printf "total [ %s ] in %s: %d\n", FS, FILENAME, tot
	if ( tot )
		{
			printf "New %s content:\n", FILENAME
			for (r=1; r<=FNR; r++)
			print all[r]
	}
}' FS="z" filename

Use nawk or /usr/xpg4/bin/awk on Solaris.