LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 05-27-2008, 02:46 AM   #1
kkpal
Member
 
Registered: Oct 2007
Posts: 101

Rep: Reputation: 15
Lightbulb bash command to count particular character in a string


Hi all

I want to count particular character(that may be letter, numeric, or any special character)in a string. And check the consecutive repetition of any special character. How can I do it?


Thanks & Regards
KKPal
 
Old 05-27-2008, 06:03 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

Is this what you are looking for:

perl -lne '$HIT++ while /a/g; END { print $HIT; }'

Example run Number of o's in foobar):
Code:
$ echo "foobar" | perl -lne '$c++ while /o/g; END { print $c; }'
2
Hope this helps.
 
Old 05-27-2008, 06:32 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
Code:
[mherring@localhost play]$ string=aarrttiimsaapocfnaa
[mherring@localhost play]$ echo $((`echo $string|sed 's/[^a]//g'|wc -m`-1))
6
Counts the # of "a"s in the variable "string"
 
1 members found this post helpful.
Old 05-27-2008, 07:14 AM   #4
kkpal
Member
 
Registered: Oct 2007
Posts: 101

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by pixellany View Post
Code:
[mherring@localhost play]$ string=aarrttiimsaapocfnaa
[mherring@localhost play]$ echo $((`echo $string|sed 's/[^a]//g'|wc -m`-1))
6
Counts the # of "a"s in the variable "string"
hi

its working.

My 2nd ques. is how I know consecutive occurrence of special symbol (.-_) in a string? i.e. "test..67"


Regards
KKPal
 
Old 05-27-2008, 07:26 AM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
For multiple occurrences of any character:
a* = any number of "a"s, including zero
a+ = 1 or more "a"s

In something like SED, certain characters have special meanings--eg "." means "any character". Thus, for a literal ".", you would need "\."

\.+ = one or more literal "."s

Suggest you go to http://tldp.org and get "Bash Guide for Beginners" or the "Advanced BASH Scripting Guide"
 
Old 05-27-2008, 07:39 AM   #6
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,564
Blog Entries: 54

Rep: Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927
Code:
string=aarrttiimsaapocfnaa; string="${string//[^a]/}"; echo ${#string}
Code:
string="a.arr.t-+ti.i.msa-apo-cfnaa"; string="${string//[a-z]/}"
for var in . - +; do str=${string//${var}/}; echo $[${#string}-${#str}]; done
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash command for removing special characters from string kkpal Linux - Newbie 5 05-26-2008 09:14 AM
command to count occurances of a string MicahCarrick Linux - Software 1 06-02-2007 11:43 PM
Bash script - passing command as string lenzj Programming 3 08-24-2006 12:36 PM
find a string followed by any word character in bash bryan.out.there Programming 2 07-12-2006 07:36 AM
[Bash] execute string as command michael_hk Linux - Newbie 2 06-26-2006 04:34 AM


All times are GMT -5. The time now is 04:56 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration