Counting Files Containing Text


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Counting Files Containing Text
# 1  
Old 04-26-2010
Counting Files Containing Text

Hello Everyone,
I'm trying to create a bash script that will count the number of files containing a certain keyword. The twist is that the keyword is found in a unix File and is part of a list. For example, the file DELETE may look like this:

John
Mike
Dana

The script will look for files containing the first keyword and then tell the user how many files contain that word. The script will then move on to the next key word and do the same thing. The filenames themselves are irrelevant.

I currently have the following code:
Code:
for i in `cat DELETE2`
do
  echo "The word $i is found in `grep -il "$i" `find .`` files"
done

Any suggestions?
# 2  
Old 04-27-2010
Just subtract 1 from the number of files found with matches and that should give you the correct number.
# 3  
Old 04-27-2010
Quote:
Originally Posted by jl487
Any suggestions?
Have you tried out, what you have shown ?

What is the error or result you got ?

A more better solution, step by step is:

Code:
for i in `cat DELETE2`
do
  for j in `find .`
  do
     grep -il "$i" $j 1>>/dev/null
     if [[ $? == 0 ]]
     then
        echo "The word $i is found in $j file"
     fi
  done
done

# 4  
Old 04-27-2010
If you want the files count (and not the exact list), you can do :
Code:
while read word
do
   count=$(grep -il "$word" $(find .) | wc -l)
   echo "The word '$word' is found in $count file(s)"
done < DELETE2

Jean-Pierre.
# 5  
Old 04-27-2010
Quote:
Originally Posted by aigles
If you want the files count (and not the exact list), you can do :
Code:
while read word
do
   count=$(grep -il "$word" $(find .) | wc -l)
   echo "The word '$word' is found in $count file(s)"
done < DELETE2

Jean-Pierre.

Everyone, thank you for your help! Aigles, I'm using a modified version of your code. Your original code gave me an incorrect output (an added file), but I was able to modify it a bit.

Thanks again!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Counting files without ls or wc

i need to write a shell script to "count the number of files in the current directory but without using either ls or wc command"..... please help! (1 Reply)
Discussion started by: lexicon
1 Replies

2. Shell Programming and Scripting

Counting 2 characters with into 2 categories from a text file

I have a tab delimited file of the following format 2 L a 2 G b 2 L c 2 G a 3 G a 3 G b 3 L c 4 L a 4 G a 4 G b 4 L c 4 G a .. ... I want to count the number of G's and L's with in the first column and the third column/categories such that I would get an output file: (6 Replies)
Discussion started by: Lucky Ali
6 Replies

3. UNIX for Advanced & Expert Users

Counting files in a given directory

Hi all, Need some help counting files... :) I'm trying to count the number of files in a given directory (and subdirectories) which reportedly contains "thousands" of files. I'm using this: ls -R | wc -l However it's been an hour and looks like it's still running; there is no output... (18 Replies)
Discussion started by: verdepollo
18 Replies

4. Shell Programming and Scripting

Counting Files

In a script, how would I go about finding the number of files for the first parameter after my script name? For instance, my script name is myscript.sh and the folder I am checking is not the current working directory, lets say it's folder1. so I type myscript.sh folder1 This script below... (2 Replies)
Discussion started by: Confirmed104
2 Replies

5. Shell Programming and Scripting

multiple files: counting

In a directory, I have 5000 multiple files that contains around 4000 rows with 10 columns in each file containing a unique string 'AT' located at 4th column. OM 3328 O BT 268 5.800 7.500 4.700 0.000 1.400 OM 3329 O BT 723 8.500 8.900... (7 Replies)
Discussion started by: asanjuan
7 Replies

6. Shell Programming and Scripting

counting prefixes of files

Hello, at the moment I'm on with programming some kind of version history script for network devices. The configration files are uploaded in the form: devicename-confg_date_time. For keeping the last 10 configurations I want to split the devicename from the rest. This works well with... (5 Replies)
Discussion started by: Sally[-_-]
5 Replies

7. Solaris

Counting up files

Hi, I have a load of if statements that look for files in a directory, I want to be able to count them up and the total files confirmed in an email? I ahve tried expr but i this does not work and it only reads in the first if and ignores the rest. Please see script, #!/bin/ksh ... (2 Replies)
Discussion started by: Pablo_beezo
2 Replies

8. Shell Programming and Scripting

Help with counting files please

Hi all. If I have a unix directory with multiple files, lets say, I have some with .dat extensions, some with .txt extensions, etc etc. How in a script would I provide a count of all the different file types (so, the different extensions, I guess) in the directory?? So if I had: test.dat... (6 Replies)
Discussion started by: gerard1
6 Replies

9. UNIX for Dummies Questions & Answers

counting files

Which one line command can count and print to the screen the number of files containing "a" or "A" in their name (5 Replies)
Discussion started by: Edy
5 Replies
Login or Register to Ask a Question