LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 01-28-2007, 03:13 PM   #1
Alinuxnoob
Member
 
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, 04:10 PM   #2
raskin
Senior Member
 
Registered: Sep 2005
Location: Russia
Distribution: NixOS (http://nixos.org)
Posts: 1,893

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, 10:07 PM   #3
Alinuxnoob
Member
 
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, 10:24 PM   #4
sundialsvcs
Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 5,261

Rep: Reputation: 1078Reputation: 1078Reputation: 1078Reputation: 1078Reputation: 1078Reputation: 1078Reputation: 1078Reputation: 1078
"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:
Code:
#!/usr/bin/python
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, 10:48 PM   #5
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 62
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.
Code:
$ grep -c of *
test1:1
test2:4
test3:3
test4:3
test5:1
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.
Code:
$ grep -c -h of *
1
4
3
3
1
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:
Code:
... | 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:
Code:
#!/usr/bin/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 ) {
                        $count++;
                }
        }
}

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


Reply

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


All times are GMT -5. The time now is 06:12 AM.

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