Counting position of a character


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Counting position of a character
# 1  
Old 11-27-2007
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
# 2  
Old 11-27-2007
requirement #1 - count z's
Code:
#!/bin/ksh
zees=$( tr -dc 'z' < inputfile)
echo ${#zees}

# 3  
Old 11-27-2007
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
# 4  
Old 11-27-2007
Something like this (change NEW to whatever you want):

Code:
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.

Last edited by radoulov; 11-28-2007 at 06:02 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count specific character of a file in each line and delete this character in a specific position

I will appreciate if you help me here in this script in Solaris Enviroment. Scenario: i have 2 files : 1) /tmp/TRANSACTIONS_DAILY_20180730.txt: 201807300000000004 201807300000000005 201807300000000006 201807300000000007 201807300000000008 2)... (10 Replies)
Discussion started by: teokon90
10 Replies

2. Shell Programming and Scripting

Counting leading spaces to a character

data.txt { "auth_type": "role", "default_attributes": { "sudoers": { i need to know how manyspaces are before an actual character in each line of a file. for example. in the above data.txt, There are 0 spaces leading up to { There are 4 spaces leading up to the... (7 Replies)
Discussion started by: SkySmart
7 Replies

3. Shell Programming and Scripting

Counting characters at each position

Hi All, here's a question from newbie I have a data like this, which set of small DNA sequences separated by new line GAATCCGGAAACAGCAACTTCAAANCA GTNATTCGGGCCAAACTGTCGAA TTNGGCAACTGTTAGAGCTCATGCGACA CCTGCTAAACGAGTTCGAGTTGAANGA TTNCGGAAGTGGTCGCTGGCACGG ACNTGCATGTACGGAGTGACGAAACCI... (6 Replies)
Discussion started by: amits22
6 Replies

4. Shell Programming and Scripting

Find character and Replace character for given position

Hi, i want find the character '-' in a file from position 284-298, if it occurs i need to replace it with 'O ' for the position in the file. How to do that using SED command. thanks in advance, Sara (9 Replies)
Discussion started by: Sara183
9 Replies

5. Shell Programming and Scripting

position of character in a line

i want to find the position of a character in a line , the first position, last, 5th occurence position , i ve tried grep -n , and expr index but they dont fit the bill. Please let me know if there is any other alternative (2 Replies)
Discussion started by: phpsnook
2 Replies

6. Shell Programming and Scripting

Identify the position of character

Hi, Can some one guide me to identify the position of a character using index in UNIX. I have a record like "17/11/2010 15:16:39;reject;10.44.48.65;daemon alert; src: 10.44.48.112; dst: 172.21.52.88" . I need to identify the value which comes after _src:_ (_ denotes space). I am able to... (15 Replies)
Discussion started by: suneel.mekala
15 Replies

7. Shell Programming and Scripting

insert character in particular position.

I want to insert space in 7th position of all the lines usign vi editor or sed command Input file 12345689010 abcdefghijk . . Output file 123456 89010 abcdef ghijk . . (7 Replies)
Discussion started by: Jairaj
7 Replies

8. UNIX for Dummies Questions & Answers

Counting occurence of a particular character on each line

Hi, I have the following data in a flat file: abcd_efgh_ijkl_20080522.dat|20080602222508|1357 abcd_efgh_ijkl_20080522.dat|20080602222508|1357 abcd_efgh_ijkl_20080522.dat|20080602222508|1357 I need to check the no. of occurence of "|" (pipe) on each line and the output should look like below:... (4 Replies)
Discussion started by: hey_mak
4 Replies

9. UNIX for Dummies Questions & Answers

How to split a value according to character position

Hello all, I have a script which picks up a series of large numbers, each of which are actually amalgamations of a series of other numbers. Unfortunately there are no separator characters so I can't use awk -F. I am looking for a way of splitting them into variables according to their... (4 Replies)
Discussion started by: michaeltravisuk
4 Replies

10. UNIX for Dummies Questions & Answers

Character position

Hi , I am required to view the fixed postion file very often . I am looking for the utility like this if the file has a one or multile line abcdefghijklmnopqr Utility should make my file look like this 12345678910111213141516-------------------------- abcdefghijk l m n o p q r ... (4 Replies)
Discussion started by: akrathi
4 Replies
Login or Register to Ask a Question