Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Count occurances of a character in a file

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 12-24-2007
Shivdatta Shivdatta is offline
Registered User
 
Join Date: Mar 2006
Last Activity: 7 January 2015, 3:22 AM EST
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 Unix and Linux 12-24-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
 
Join Date: Mar 2005
Last Activity: 9 April 2015, 4:19 AM EDT
Location: classification algos
Posts: 3,206
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 Unix and Linux 12-24-2007
Shivdatta Shivdatta is offline
Registered User
 
Join Date: Mar 2006
Last Activity: 7 January 2015, 3:22 AM EST
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 Unix and Linux 12-24-2007
ghostdog74 ghostdog74 is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 28 January 2015, 8:30 AM EST
Posts: 2,669
Thanks: 0
Thanked 18 Times in 18 Posts

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

Sponsored Links
    #5  
Old Unix and Linux 12-24-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
 
Join Date: Mar 2005
Last Activity: 9 April 2015, 4:19 AM EDT
Location: classification algos
Posts: 3,206
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 Unix and Linux 12-24-2007
drl's Unix or Linux Image
drl drl is offline Forum Advisor  
Registered Voter
 
Join Date: Apr 2007
Last Activity: 22 May 2015, 3:57 PM EDT
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 1,773
Thanks: 59
Thanked 234 Times in 210 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 Unix and Linux 12-24-2007
cfajohnson's Unix or Linux Image
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
 
Join Date: Mar 2007
Last Activity: 5 April 2015, 4:53 PM EDT
Location: Toronto, Canada
Posts: 2,882
Thanks: 0
Thanked 117 Times in 106 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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 04:47 AM.