LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-01-2012, 11:45 AM   #1
TKH
Member
 
Registered: Jul 2011
Location: Milky Way
Distribution: Ubuntu, LFS, Slackware, Fedora
Posts: 223

Rep: Reputation: 20
Character counting


Hi, Fellow LQers

I'm trying to make a simple program which can count the number of certain characters (i.e. backslash, dash, or underscore).
However, I don't know what program should I use, let alone what to type. Can someone help me?

Thank you
 
Old 08-01-2012, 12:06 PM   #2
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
Some possible ideas here : http://www.unix.com/shell-programmin...cters-sed.html
 
Old 08-01-2012, 12:28 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
To count the number of dashes:

Code:
$ text='foo-bar-baz'
$ printf '%s' "$text" | tr -dc '-' | wc -c
2

$ text='foo-bar-baz'
$ char=${text//[^-]}
$ echo ${#char}
2
The first uses standard coreutils, and should be portable across systems.

The second uses a parameter substitution that's only available to advanced shells like bash or ksh. On the plus side, it's all built into the shell, and therefore very fast.

The concept is the same in both cases. Remove everything except the characters you're interested in, and count whatever remains.

Last edited by David the H.; 08-01-2012 at 12:29 PM.
 
1 members found this post helpful.
Old 08-01-2012, 02:38 PM   #4
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
This may do the trick...
Code:
sed 's/./&\n/g' $InFile |sort|uniq -c > $OutFile
Daniel B. Martin
 
1 members found this post helpful.
Old 08-03-2012, 12:50 AM   #5
TKH
Member
 
Registered: Jul 2011
Location: Milky Way
Distribution: Ubuntu, LFS, Slackware, Fedora
Posts: 223

Original Poster
Rep: Reputation: 20
Thank you for your helps, it worked!
 
Old 08-03-2012, 02:05 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
How many ways to skin this puppy:
Code:
text='foo-bar-baz'

grep -o '-' <<<$text | wc -l

awk -F- '{print NF-1}' <<<$text

ruby -ne 'puts $_.scan(/-/).length' <<<$text
 
  


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
[SOLVED] Character counting with sed/wc not working as expected. standard_output Linux - Newbie 6 04-27-2012 08:15 AM
Counting to a specific character mikehalfogre Programming 9 02-12-2010 02:12 PM
Bash: Counting the number of character occurences in a variable basildon Linux - Newbie 3 09-22-2008 10:11 AM
Character counting in Vi nirvana4ol Linux - General 8 12-18-2007 05:04 AM
Javascript character counting of a dynamic PHP-generated form benrose111488 Programming 2 06-02-2007 02:48 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:48 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