LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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, 01: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, 05:03 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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, 05:32 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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, 06: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, 06:26 AM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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, 06:39 AM   #6
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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



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 08:14 AM
command to count occurances of a string MicahCarrick Linux - Software 1 06-02-2007 10:43 PM
Bash script - passing command as string lenzj Programming 3 08-24-2006 11:36 AM
find a string followed by any word character in bash bryan.out.there Programming 2 07-12-2006 06:36 AM
[Bash] execute string as command michael_hk Linux - Newbie 2 06-26-2006 03:34 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:07 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration