Computer Hope

Software => BSD, Linux, and Unix => Topic started by: aggressivebloodcell on June 26, 2007, 04:39:38 PM

Title: grep --help!
Post by: aggressivebloodcell on June 26, 2007, 04:39:38 PM
Hey all,

I need some help using grep.  How would I do a word count for specific words from a file.  Lets say I am searching for apples, bannanas and grapes from a text file and need to output the frequency of those words.  I do not want to do a grep of those words one by one. 

Much Appreciated,

abc
Title: Re: grep --help!
Post by: ghostdog74 on June 27, 2007, 12:07:32 AM
grep only count lines that contains your word. If two identical words are on the same line, it will still count as 1.
you might want to use awk.
Code: [Select]
awk 'BEGIN{FS=",";c=0; search="word"}
     $0~search{ for (i=1;i<=NF;i++) {
               if ($i == search) {
           c++
       }
   }
     } END{print "Total count of "search " is: "c}' file