0

There is a similar question to mine already but mine has a slight difference. I want to see how many files contain each word. When a file has a word then it increments the counter and then continues to the next file. I do not want to know how many times the word appears in each file or the name of the file it appears in, just a counter saying how many files the word is found in my files. For example: I have a directory with 10 files, each file is a recipe and i want to see how many files contain the word 'salt'. After running the command it would output something like : 9 Salt. Meaning it was found in 9 out of 10 files. I want this for everyword though. So far I have this:

 grep -l *| tr '[A-Z]' '[a-z]' | tr -d '[:punct:]' | tr ' ' '\n' | sort | uniq | wc -l

Any help is much appreciated.

1

Let's say you recipes folder in in /home/username/recipes

Here is a simple script:

#!/bin/bash

count=0
dir=/home/username/recipes/
for recipe in $(ls $dir); do
  if [ $(cat $HOME/recipes/$recipe | grep $1) ]; then
   ((count++))
  fi
done
echo "$count $1"

Once you've saved the file, you have to make it executable. Do this with:

chmod 740 script.sh

When you run it, include the argument you are looking for, like this:

./script.sh salt

If it matches in two files, it will output:

2 salt

If it matches in three files, it will output:

3 salt

etc.

  • I understand what you are doing and how it works. The problem for me is I am not allowed to submit something which uses loops and if then statements. We have only been shown the commands used in terminals directly. I might have put this in the wrong tag, sorry. Is there a way to use commands like grep that will work ? Thanks very much for all your help. – Demostroyer Dec 11 '14 at 15:25
  • This answer is using grep. I couldn't imagine trying to do something like this without loops though, sorry. Way too much expended effort for no reason. – cremefraiche Dec 11 '14 at 15:31
  • Thanks man. I agree it is, but for my submission, it is not allowed which is a lot of effort for something which can be done so much quicker with other ways. I will use your answer as a way to further my studies though. – Demostroyer Dec 11 '14 at 15:56
  • I am glad you could get some use from it, good luck with your studies. – cremefraiche Dec 11 '14 at 16:05
1

Use grep -c

For example

kaustubh@hacked:~/test$ cat a
salt
salt
salt
salt
salt

kaustubh@hacked:~/test$ cat b
here sis salt
their is salt
and everywhere is salt

kaustubh@hacked:~/test$ cat c
salt hash
hash salt
sweet salt

kaustubh@hacked:~/test$ cat d
this is line salt

Now I want count of word salt in files: grep -c salt *

kaustubh@hacked:~/test$ grep -c  salt *
a:5
b:3
c:3
d:1
  • total=0;for occurance in $(grep -RcshF salt ~/test/); do let total+=$occurance ; done ; echo $total – Costas Dec 11 '14 at 12:49
  • @Security Beast, if i want to use this for every word, do I have to change much in your 'grep -c salt *'. I would need to change the 'salt' part i know. – Demostroyer Dec 11 '14 at 15:15
  • just replace salt with you desire word it it will give you count – Security Beast Dec 11 '14 at 15:43
  • I need to only print the total number of files it appears in, so say 5 out of 10 files it would have: 5 salt, meaning salt appeared in five of the files. – Demostroyer Dec 11 '14 at 15:55
  • <code>grep -c salt | awk -F ":" '{print $1}'</code> This will give you file names if you just want file count then <code>grep -c salt | awk -F ":" '{print $1}' | wc -l </code> This will give you count – Security Beast Dec 11 '14 at 16:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.