Web Hosting Talk







View Full Version : Script needed for counting words from logfiles!


NightMan
03-29-2002, 06:41 AM
Anyone have a script (perl/shell) to count a given word in any file(i.e maillog)

Tried at google, but didn't find anything useful

Walter
03-29-2002, 06:54 AM
Why not use

grep -c word filename

or

grep -ci word filename

for ignoring case...

jstanden
03-30-2002, 02:57 AM
In most *nix systems (Linux/FreeBSD/etc) "wc" is the word count command.

wc -w [file] for words
wc -l [file] for lines
wc [file] for lines, words, characters

for example:

wc -w myfile.log

Should give you what you're looking for.

<Just re-read the post and noticed you want the count for a specific word, I knew the question seemed too easy :D>

NightMan
03-30-2002, 03:15 AM
Thank you guys,
What I am trying is someway put this function to be excuted from browser. like using pl or cgi script. to be used with any given word.

any suggestion?

grep -ci word filename works fine. but how do I do it from browser side?

bitserve
03-30-2002, 05:21 AM
#!/usr/bin/perl

$filename = "foo.txt";
$string = "bar";
$count = 0;

undef $/;
open(FILE, "$filename");
while (<FILE>) {
while ($_ =~ /$string/gi) {
$count++;
}
}
close (FILE);
$/ = "\n";
print "Content-type: text/plain\n\n";
print "$count\n";


If you meant a given word in a given file.