Count occurances of a character in a file | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Count occurances of a character in a file

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-24-2007
Shivdatta Shivdatta is offline
Registered User
 
Join Date: Mar 2006
Last Activity: 20 May 2014, 1:42 AM EDT
Posts: 104
Thanks: 0
Thanked 1 Time in 1 Post
Count occurances of a character in a file

I want to find the number of occurences of a character in a file.
How do i do it.

Eg:
$cat file1.txt
Welcome to World of Unix.
$

If i want to find the occurences of 'o' then I should be getting 3.

Thanks.
Sponsored Links
    #2  
Old 12-24-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
 
Join Date: Mar 2005
Last Activity: 5 July 2014, 11:14 AM EDT
Location: classification algos
Posts: 3,203
Thanks: 19
Thanked 30 Times in 27 Posts

Code:
echo "start with this" | awk '{ for ( i=1; i<=length; i++ ) arr[substr($0, i, 1)]++ }END{ for ( i in arr ) { print i, arr[i] } }'

Sponsored Links
    #3  
Old 12-24-2007
Shivdatta Shivdatta is offline
Registered User
 
Join Date: Mar 2006
Last Activity: 20 May 2014, 1:42 AM EDT
Posts: 104
Thanks: 0
Thanked 1 Time in 1 Post
Thanks buddy,

Can you explain me where the actual conting of string happens..
I am unable to understand the script
    #4  
Old 12-24-2007
ghostdog74 ghostdog74 is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 7 November 2013, 6:42 AM EST
Posts: 2,651
Thanks: 0
Thanked 17 Times in 17 Posts

Code:
 # awk 'BEGIN{FS=""}{array[$1]++}END{ # code to print values}' file

Sponsored Links
    #5  
Old 12-24-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
 
Join Date: Mar 2005
Last Activity: 5 July 2014, 11:14 AM EDT
Location: classification algos
Posts: 3,203
Thanks: 19
Thanked 30 Times in 27 Posts
Quote:
Originally Posted by Shivdatta View Post
Thanks buddy,

Can you explain me where the actual conting of string happens..
I am unable to understand the script
Extract each character and create an associative array which maps between the character and the count of character
Sponsored Links
    #6  
Old 12-24-2007
drl's Avatar
drl drl is offline Forum Advisor  
Registered Voter
 
Join Date: Apr 2007
Last Activity: 23 July 2014, 8:45 AM EDT
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 1,661
Thanks: 33
Thanked 185 Times in 169 Posts
Hi, Shivdatta.

Do you wish to count occurrences of all characters or of a specific character? ... cheers, drl

Last edited by drl; 12-24-2007 at 07:02 AM..
Sponsored Links
    #7  
Old 12-24-2007
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
 
Join Date: Mar 2007
Last Activity: 18 June 2014, 3:49 PM EDT
Location: Toronto, Canada
Posts: 2,875
Thanks: 0
Thanked 110 Times in 102 Posts
Quote:
Originally Posted by Shivdatta View Post
I want to find the number of occurences of a character in a file.
How do i do it.

Eg:
$cat file1.txt
Welcome to World of Unix.
$

If i want to find the occurences of 'o' then I should be getting 3.


Code:
tr -cd o file1.txt | wc -c

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Replace multiple occurances of same character with a single character. dipanchandra Shell Programming and Scripting 3 12-20-2010 01:17 AM
How to count number of occurances of string in a file? Shirisha Shell Programming and Scripting 7 10-13-2010 06:21 AM
Count occurances of X Y Z in a file in 1 go. msullivan Shell Programming and Scripting 13 03-30-2009 11:38 AM
Merging lines based on occurances of a particular character in a file mohan_tuty Shell Programming and Scripting 2 07-15-2008 01:14 AM
Count occurances in a file samit_9999 UNIX for Dummies Questions & Answers 2 04-23-2008 10:56 AM



All times are GMT -4. The time now is 02:05 PM.