Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: grep --help!  (Read 7626 times)

0 Members and 1 Guest are viewing this topic.

aggressivebloodcell

  • Guest
grep --help!
« 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

ghostdog74



    Specialist

    Thanked: 27
    Re: grep --help!
    « Reply #1 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