Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 01-28-2007, 04:13 PM   #1
Registered: Feb 2002
Location: Van city
Distribution: Ubuntu
Posts: 204

Rep: Reputation: 30
How to create a bash script to count?

I have no bash script knowledge.
Hi I was wondering how does one go abouts creating a bash script that will allow me to search a folder that will count how many times does a XXX letter show up in that folder. So I would need to search in that folder that contains txt's files that contains random letters for example "XXX, XYE, XE, Z, NO"

Script scans and finds

XE shows 5x
NO shows 1x

and maybe show only anything that has been found more than once....
Old 01-28-2007, 05:10 PM   #2
Senior Member
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (
Posts: 1,900

Rep: Reputation: 68
I recommend reading info bash, info grep, info wc, info sort, info uniq.
Something like 'grep -o letter file' (-i if case independent) can be piped ("|") to "wc -l" to count occurences. It all can be inside "$( )" to be assigned to variable or to be first argument to "echo" with letter being second. You can do it inside "for" cycle over list of all words you search for and pipe entire output (put "|" after "done" without semicolon) to "sort -k 1n" and then possibly "egrep -v '^1 '" (single quotes inside are significant). You can iterate it over all files by wildcard and "for".
Old 01-28-2007, 11:07 PM   #3
Registered: Feb 2002
Location: Van city
Distribution: Ubuntu
Posts: 204

Original Poster
Rep: Reputation: 30
I'm sorry I was wondering if there is free code that already does this that I can download. Don't have the time to learn how to write a bash script. Where can I find a script that does this in java or any other programming language.
Old 01-28-2007, 11:24 PM   #4
LQ Guru
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 8,679
Blog Entries: 4

Rep: Reputation: 3023Reputation: 3023Reputation: 3023Reputation: 3023Reputation: 3023Reputation: 3023Reputation: 3023Reputation: 3023Reputation: 3023Reputation: 3023Reputation: 3023
"Patience, padewan ..."

If you are truly anxious to receive results, then perhaps the first thing you need to recognize is that "an executable file" can be written in any language, not just the script language that is built-in to bash!

When the first line of a script-file starts with "#!" (known in unix circles as she-bang...), followed by the name of a program, this specifies the name of the program that should be executed in order to 'run' this script. So a script can be implemented in any language, not just "bash script."

And, having said that, let me be very quick to add that I personally have very little use whatsoever for "bash scripts!"

Most of my scripts start with this line:
If your "language de jour" is Java ... cool! Just change the first line!

"Your mileage may vary," but that is exactly the point! Unix/Linux environments don't limit you to "just one way of doing things." (If you come from a Windows world, that takes a little getting used to.)
Old 01-28-2007, 11:48 PM   #5
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
grep can search many files at once, but by default it will output the file name as well as the number of lines which matched the pattern you are looking for.
$ grep -c of *
Note that this will not count multiple occurrences of the pattern on one line. Assuming this is OK, the question is now how to extract these numbers and add them up. You can ask grep not to print the file names using the -h option, or you could pass the output of "grep -c pattern *" through cut, chopping the lines on the delimiting character : and printing the second field. See the cut manual page for details.

Using -h is easier, and invokes less processes - a good rule of thumb when constructing shell scripts is to try to invoke as few processes as possible - this will improve performance.
$ grep -c -h of *
Right, so how to add it up? Well, Linux has lots of useful utilities which you can generally count on to be installed. Here's one method:
... | awk '{ total += $1 } END { print total }'
...which will read a list of numbers, and print out the sum of their values.
The thing about this is that awk is actually a pretty functional language interpreter, and if you're going to invoke it at all, you might as well use it to do the pattern matching as well and dispense with a shell script entirely. Doing this will also help you to check for multiple instances of your pattern.

One upon a time I used to write quite a lot of awk programs, some of which were non-trivial. Then, one day I put one of the simpler ones through a2p, which is an automatic code translation program which read an awk program, and spits out a perl program that (with luck) does the same thing.

This awk script I had tested it on operated on pretty large data sets, and took several minutes to run. The Perl version took about half as long to run. From that day on, I've pretty much used Perl instead of awk. Learn it - it's great.

OK, so here's how I'd do it in Perl:

use strict;
use warnings;

my $pattern = shift || die "you must specify a pattern as the first argument\n";
my $count = 0;

while(<>) {
        @_ = split(/\s+/);
        foreach my $word (@_) {
                # note, remove the i to make the check case sensitive
                if ( $word =~ /$pattern/i ) {

print "total number of instance of $pattern is $count\n";
I hope you will do your own implementation.


bash, count, counting, script, scripts

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
create a bash script that restarts? nny0000 Linux - Software 7 09-15-2006 06:14 PM
Cannot create folders with bash script called from php keyF Linux - Software 4 06-25-2006 11:58 AM
File count with script? fbfd1338 Linux - Newbie 4 10-23-2005 04:02 AM
How to create a bash script to automatically disown a process. jon_k Linux - Software 5 06-19-2005 06:53 AM
How do I create application launcher using bash script msgclb Programming 2 01-30-2005 07:28 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:41 AM.

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