Page 1 of 2 1 2 LastLast
Results 1 to 10 of 19

Thread: Howto count a specific letter or word in a text file

  1. #1
    Senior Member
    Join Date
    Apr 2011
    Posts
    113
    Thanks
    18
    Thanked 0 Times in 0 Posts
    Rep Power
    4

    Default Howto count a specific letter or word in a text file

    Hi,

    i want to calculate a specific word from text document ...using shell script

    If abc.txt contain the data-

    ===================================

    Hi Hello Good Morning.
    Good evening .
    ==================================

    and here i want count the "Good" word using bash shellscript.
    then how to write this code. plz give me simple example.

    Thanks in advance...

    Regards,
    Priyanka

  2. #2
    Junior Member
    Join Date
    Jun 2011
    Posts
    12
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Rep Power
    0

    Default

    Hi,
    You can do this by using this command:
    grep -w "Good" file_name | wc -l

    Thanks,
    Kunal
    Last edited by kunal_renu; 9th June 2011 at 11:16 PM.

  3. #3
    Contributors jaysunn's Avatar
    Join Date
    Apr 2009
    Location
    NYC - USA
    Posts
    1,023
    Thanks
    164
    Thanked 153 Times in 132 Posts
    Rep Power
    24

    Default

    Another way in a script.

    Code:
    #!/bin/bash
    
    while read line
    
    do 
    
    grep -i "good" | wc -l 
    
    done < abc.txt
    Code:
    [jralph@jralph-linux ~]$ ./script.sh 
    
    2
    Jaysunn

  4. #4
    Never say die nixcraft's Avatar
    Join Date
    Jan 2005
    Location
    BIOS
    Posts
    4,500
    Thanks
    17
    Thanked 789 Times in 507 Posts
    Rep Power
    10

    Default

    Quote Originally Posted by kunal_renu View Post
    Hi,
    You can do this by using this command:
    grep -w "Good" file_name | wc -l

    Thanks,
    Kunal
    Keep it simple, no need to use the cat command:
    Code:
    grep -cw 'Good' input
    All [Solved] threads are closed by mods / admin to avoid spam issues. See Howto mark a thread as [Solved]


  5. #5
    Member Shell_Life's Avatar
    Join Date
    May 2011
    Location
    Bahia, Brazil
    Posts
    99
    Thanks
    0
    Thanked 11 Times in 11 Posts
    Rep Power
    4

    Default

    The code:
    Code:
     
    #!/bin/bash
     
    while read line
     
    do 
     
    grep -i "good" | wc -l 
     
    done < abc.txt
    Does not work properly.

    It will always bypass the first line.

    Here is a test data:
    Code:
     
    Good morning
    Testing
    It will give zero as result.

  6. #6
    Senior Member
    Join Date
    Apr 2011
    Posts
    113
    Thanks
    18
    Thanked 0 Times in 0 Posts
    Rep Power
    4

    Default

    Thanks to all for helping me
    i have tried all above code but i am getting the output of Count "Good" word only 1 not 2

    Regards,
    Priyanka
    Last edited by aish; 10th June 2011 at 12:45 PM.

  7. #7
    Junior Member
    Join Date
    Jun 2011
    Posts
    12
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Rep Power
    0

    Default

    Quote Originally Posted by pspriyanka View Post
    Thanks to all for helping me
    i have tried all above code but i am getting the output of Count "Good" word only 1 not 2

    Regards,
    Priyanka
    Hi Priyanka,
    As you only want to do this from script. So Please use this script.
    *******************
    #!/bin/bash
    k=0
    while read line
    do
    s=`echo $line |grep -w Good | wc -l`
    k=`expr $s + $k`
    done < test
    echo $k
    **************************
    Contents of the test text file is :
    **********************
    Good evening .

    Hi Hello Good Morning.
    Hi Hello Good Morning.
    Hi Hello Good Morning.
    Hi Hello Good Morning.
    Hi Hello Good Morning.
    Hi Hello Good Morning.
    *******************
    Please notice that there are 8 lines but the occurance of "Good" string is 7
    So the result of my script is :
    *****************
    ./test.sh
    7
    ********************
    Please note that i am using "-w" with grep command so It will search for exact string that is only for "Good". So if you do not know about the case of the string you can use "-i" option for grep command

    Thanks and Regards,
    Kunal
    Last edited by kunal_renu; 10th June 2011 at 04:39 PM.

  8. The Following User Says Thank You to kunal_renu For This Useful Post:

    aish (24th June 2011)

  9. #8
    Contributors jaysunn's Avatar
    Join Date
    Apr 2009
    Location
    NYC - USA
    Posts
    1,023
    Thanks
    164
    Thanked 153 Times in 132 Posts
    Rep Power
    24

    Default

    @shell_life

    That is very interesting. I have never noticed that the while loop bypasses the first line. Can someone please let me know why this is the case? I have tested with his test data and in fact this is the case. Every google search on how to read a file in line by line shows the while read method. Please help me understand this.

    I have also tried kunal_renu's code and it in fact skips the first line.
    jaysunn

  10. #9
    Junior Member
    Join Date
    Jun 2011
    Posts
    12
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Rep Power
    0

    Default

    Quote Originally Posted by jaysunn View Post
    @shell_life

    That is very interesting. I have never noticed that the while loop bypasses the first line. Can someone please let me know why this is the case? I have tested with his test data and in fact this is the case. Every google search on how to read a file in line by line shows the while read method. Please help me understand this.

    I have also tried kunal_renu's code and it in fact skips the first line.
    jaysunn
    Hi Jaysunn,
    There is nothing wrong with while loop. It does not bypass the first line.
    Can you please tell me the result of my script as I don't think you will face
    problem with my script as i am using each line and storing the occurance into a varaible.
    If this would be the case ( While loop bypass the first line):
    Below script also bypass first line:
    Contests of test file :
    **********************

    Good morning
    Good
    hi
    kunal
    kunal
    Good
    Testing -----Note first line is empty
    **************************
    my script test1.sh:
    ****************************
    #!/usr/bin/bash while read line
    do
    echo $line
    done < test
    ****************************
    Result of my script: ./test1.sh

    Good morning
    Good
    hi
    kunal
    kunal
    Good
    Testing
    **********************************
    I think issue is with your script as grep is not getting input while running
    very first time (therefore it is skipping that line)
    Thanks and Regards,
    Kunal
    Last edited by kunal_renu; 10th June 2011 at 08:03 PM.

  11. #10
    Contributors jaysunn's Avatar
    Join Date
    Apr 2009
    Location
    NYC - USA
    Posts
    1,023
    Thanks
    164
    Thanked 153 Times in 132 Posts
    Rep Power
    24

    Default

    @kunal_renu,
    Ok it appears that my while loop is missing the first line. Yours I tested and it looks ok. Still I find it odd that on mine it misses to first Good.

    PHP Code:
    [jralph@jralph-linux ~]$ cat file
    Good morning
    Good
    hi
    kunal
    kunal
    Good

    [jralph@jralph-linux ~]$ cat script.sh

    #!/bin/bash
    while read line
    do 
    grep -"good" wc -
    done 
    file

    [jralph@jralph-linux ~]$ ./script.sh 


    I find this odd. My expected result would be 3.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Unix / Linux: Perl count null word in Excel Spreadsheet
    By aish in forum Coding in General
    Replies: 2
    Last Post: 16th March 2012, 03:41 PM
  2. Perl- count of same word in log file
    By aish in forum Coding in General
    Replies: 0
    Last Post: 13th May 2011, 05:39 PM
  3. find a word in a file, and change a word beneath it ??
    By vikas027 in forum Shell scripting
    Replies: 3
    Last Post: 10th June 2010, 09:38 AM
  4. Replies: 6
    Last Post: 14th April 2010, 12:26 PM
  5. Shell Script To change strings / text in a text file
    By jaysunn in forum Shell scripting
    Replies: 1
    Last Post: 8th May 2009, 05:58 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •