Grep : Find all possible cases of a word in text file

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












I have a directory with many text files.



Out of these files, I am interested in a word "abcdefghi". I need to list all possible cases of this word such as



  • abcdefghi

  • abcdefghI

  • abcDefghi

  • ABCDEFGHI

and all other possible combinations.



It is possible with grep or egrep?



I know, I can write a shell script with combos of grep and inverse grep, unique and achieve the outputs, but I am looking for portable solution.










share|improve this question



















  • 2




    Have you tried the man page?
    – Joseph R.
    May 10 '14 at 14:31











  • Tried, failed. Maybe too dumb to make sense of man pages.
    – user1263746
    May 10 '14 at 14:32






  • 1




    What you want is called case-insensitive match. Use the -iswitch for that.
    – Joseph R.
    May 10 '14 at 14:36










  • @JosephR. It will print while whole line, I just need the matched word. See Avinash's answer below.
    – user1263746
    May 10 '14 at 14:44














up vote
1
down vote

favorite












I have a directory with many text files.



Out of these files, I am interested in a word "abcdefghi". I need to list all possible cases of this word such as



  • abcdefghi

  • abcdefghI

  • abcDefghi

  • ABCDEFGHI

and all other possible combinations.



It is possible with grep or egrep?



I know, I can write a shell script with combos of grep and inverse grep, unique and achieve the outputs, but I am looking for portable solution.










share|improve this question



















  • 2




    Have you tried the man page?
    – Joseph R.
    May 10 '14 at 14:31











  • Tried, failed. Maybe too dumb to make sense of man pages.
    – user1263746
    May 10 '14 at 14:32






  • 1




    What you want is called case-insensitive match. Use the -iswitch for that.
    – Joseph R.
    May 10 '14 at 14:36










  • @JosephR. It will print while whole line, I just need the matched word. See Avinash's answer below.
    – user1263746
    May 10 '14 at 14:44












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have a directory with many text files.



Out of these files, I am interested in a word "abcdefghi". I need to list all possible cases of this word such as



  • abcdefghi

  • abcdefghI

  • abcDefghi

  • ABCDEFGHI

and all other possible combinations.



It is possible with grep or egrep?



I know, I can write a shell script with combos of grep and inverse grep, unique and achieve the outputs, but I am looking for portable solution.










share|improve this question















I have a directory with many text files.



Out of these files, I am interested in a word "abcdefghi". I need to list all possible cases of this word such as



  • abcdefghi

  • abcdefghI

  • abcDefghi

  • ABCDEFGHI

and all other possible combinations.



It is possible with grep or egrep?



I know, I can write a shell script with combos of grep and inverse grep, unique and achieve the outputs, but I am looking for portable solution.







grep






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 10 '14 at 16:49









Gilles

520k12610391569




520k12610391569










asked May 10 '14 at 14:28









user1263746

1993517




1993517







  • 2




    Have you tried the man page?
    – Joseph R.
    May 10 '14 at 14:31











  • Tried, failed. Maybe too dumb to make sense of man pages.
    – user1263746
    May 10 '14 at 14:32






  • 1




    What you want is called case-insensitive match. Use the -iswitch for that.
    – Joseph R.
    May 10 '14 at 14:36










  • @JosephR. It will print while whole line, I just need the matched word. See Avinash's answer below.
    – user1263746
    May 10 '14 at 14:44












  • 2




    Have you tried the man page?
    – Joseph R.
    May 10 '14 at 14:31











  • Tried, failed. Maybe too dumb to make sense of man pages.
    – user1263746
    May 10 '14 at 14:32






  • 1




    What you want is called case-insensitive match. Use the -iswitch for that.
    – Joseph R.
    May 10 '14 at 14:36










  • @JosephR. It will print while whole line, I just need the matched word. See Avinash's answer below.
    – user1263746
    May 10 '14 at 14:44







2




2




Have you tried the man page?
– Joseph R.
May 10 '14 at 14:31





Have you tried the man page?
– Joseph R.
May 10 '14 at 14:31













Tried, failed. Maybe too dumb to make sense of man pages.
– user1263746
May 10 '14 at 14:32




Tried, failed. Maybe too dumb to make sense of man pages.
– user1263746
May 10 '14 at 14:32




1




1




What you want is called case-insensitive match. Use the -iswitch for that.
– Joseph R.
May 10 '14 at 14:36




What you want is called case-insensitive match. Use the -iswitch for that.
– Joseph R.
May 10 '14 at 14:36












@JosephR. It will print while whole line, I just need the matched word. See Avinash's answer below.
– user1263746
May 10 '14 at 14:44




@JosephR. It will print while whole line, I just need the matched word. See Avinash's answer below.
– user1263746
May 10 '14 at 14:44










2 Answers
2






active

oldest

votes

















up vote
6
down vote



accepted










With GNU grep, try this:



grep -io -- 'abcdefghi' *.txt


I assumed all the files you want to search for a particular text would be ended with .txt (and you don't want the hidden ones).



From man grep on a system where grep is GNU's implementation (as is typical on Linux-based systems).



-o, --only-matching show only the part of a line matching PATTERN
-i, --ignore-case ignore case distinctions





share|improve this answer






















  • It appends name of the matched file before the output. Is there any way to suppress that? Except that the solutions is perfect.
    – user1263746
    May 10 '14 at 14:35










  • @user1263746 To do what you want cat *.txt | grep ...
    – Joseph R.
    May 10 '14 at 14:37










  • yes you can.grep -io 'rah' *.txt | awk -F: ' print $2'. This command removes the file-name from the output.
    – Avinash Raj
    May 10 '14 at 14:37







  • 1




    grep -ioh does the job :) Thanks guys!
    – user1263746
    May 10 '14 at 14:45










  • @AvinashRaj: This doesn't find the combinations of string as the OP want.
    – cuonglm
    May 10 '14 at 15:55

















up vote
1
down vote













As a beginner in Bash scripting I was looking exactly for this, and based on the accepted answer above, I wrote the following Nautilus script, which I named "Search Text in Directory...". As this will be useful for me from time to time, I thought it might also be useful for others as well.



#!/bin/bash
# Nautilus Script to search text in selected folder
# Determine the path
if [ -e -n $1 ]; then
obj="$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS"
else
base="`echo $NAUTILUS_SCRIPT_CURRENT_URI | cut -d'/' -f3- | sed 's/%20/ /g'`"
obj="$base/$1##*/"
fi
# Determine the type and go
if [ -f "$obj" ]; then
/usr/bin/canberra-gtk-play --id="dialog-error" &
zenity --error --title="Search Directory" --text "Sorry, selected item is not a folder."
elif [ -d "$obj" ]; then
cd "$obj"
# Get text to search
SearchText=$(zenity --entry --title="Search Directory" --text="For Text:" --width=250)
if [ -z "$SearchText" ]; then
notify-send "Search Directory" "Nothing entered; exiting..." -i gtk-dialog-info -t 500 -u normal &
exit
else
if [ -f "/tmp/Search-Directory-Results.txt" ]; then
rm "/tmp/Search-Directory-Results.txt"
fi
grep_menu()

im="zenity --list --radiolist --title="Search Directory" --text="Please select one of the search options below:""
im=$im" --column="☉" --column "Option" --column "Description" "
im=$im"TRUE "case-sensitive" "Match only: Text" "
im=$im"FALSE "case-insensitive" "Match: TEXT, text, Text..." "

grep_option()

choice=`echo $im
grep_menu
grep_option
fi
zenity --class=LIST --text-info
--editable
--title="Search Directory"
--filename="/tmp/Search-Directory-Results.txt"
fi
exit 0





share|improve this answer




















    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f128792%2fgrep-find-all-possible-cases-of-a-word-in-text-file%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    6
    down vote



    accepted










    With GNU grep, try this:



    grep -io -- 'abcdefghi' *.txt


    I assumed all the files you want to search for a particular text would be ended with .txt (and you don't want the hidden ones).



    From man grep on a system where grep is GNU's implementation (as is typical on Linux-based systems).



    -o, --only-matching show only the part of a line matching PATTERN
    -i, --ignore-case ignore case distinctions





    share|improve this answer






















    • It appends name of the matched file before the output. Is there any way to suppress that? Except that the solutions is perfect.
      – user1263746
      May 10 '14 at 14:35










    • @user1263746 To do what you want cat *.txt | grep ...
      – Joseph R.
      May 10 '14 at 14:37










    • yes you can.grep -io 'rah' *.txt | awk -F: ' print $2'. This command removes the file-name from the output.
      – Avinash Raj
      May 10 '14 at 14:37







    • 1




      grep -ioh does the job :) Thanks guys!
      – user1263746
      May 10 '14 at 14:45










    • @AvinashRaj: This doesn't find the combinations of string as the OP want.
      – cuonglm
      May 10 '14 at 15:55














    up vote
    6
    down vote



    accepted










    With GNU grep, try this:



    grep -io -- 'abcdefghi' *.txt


    I assumed all the files you want to search for a particular text would be ended with .txt (and you don't want the hidden ones).



    From man grep on a system where grep is GNU's implementation (as is typical on Linux-based systems).



    -o, --only-matching show only the part of a line matching PATTERN
    -i, --ignore-case ignore case distinctions





    share|improve this answer






















    • It appends name of the matched file before the output. Is there any way to suppress that? Except that the solutions is perfect.
      – user1263746
      May 10 '14 at 14:35










    • @user1263746 To do what you want cat *.txt | grep ...
      – Joseph R.
      May 10 '14 at 14:37










    • yes you can.grep -io 'rah' *.txt | awk -F: ' print $2'. This command removes the file-name from the output.
      – Avinash Raj
      May 10 '14 at 14:37







    • 1




      grep -ioh does the job :) Thanks guys!
      – user1263746
      May 10 '14 at 14:45










    • @AvinashRaj: This doesn't find the combinations of string as the OP want.
      – cuonglm
      May 10 '14 at 15:55












    up vote
    6
    down vote



    accepted







    up vote
    6
    down vote



    accepted






    With GNU grep, try this:



    grep -io -- 'abcdefghi' *.txt


    I assumed all the files you want to search for a particular text would be ended with .txt (and you don't want the hidden ones).



    From man grep on a system where grep is GNU's implementation (as is typical on Linux-based systems).



    -o, --only-matching show only the part of a line matching PATTERN
    -i, --ignore-case ignore case distinctions





    share|improve this answer














    With GNU grep, try this:



    grep -io -- 'abcdefghi' *.txt


    I assumed all the files you want to search for a particular text would be ended with .txt (and you don't want the hidden ones).



    From man grep on a system where grep is GNU's implementation (as is typical on Linux-based systems).



    -o, --only-matching show only the part of a line matching PATTERN
    -i, --ignore-case ignore case distinctions






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited May 10 '14 at 14:55









    Stéphane Chazelas

    293k54550891




    293k54550891










    answered May 10 '14 at 14:32









    Avinash Raj

    2,57731127




    2,57731127











    • It appends name of the matched file before the output. Is there any way to suppress that? Except that the solutions is perfect.
      – user1263746
      May 10 '14 at 14:35










    • @user1263746 To do what you want cat *.txt | grep ...
      – Joseph R.
      May 10 '14 at 14:37










    • yes you can.grep -io 'rah' *.txt | awk -F: ' print $2'. This command removes the file-name from the output.
      – Avinash Raj
      May 10 '14 at 14:37







    • 1




      grep -ioh does the job :) Thanks guys!
      – user1263746
      May 10 '14 at 14:45










    • @AvinashRaj: This doesn't find the combinations of string as the OP want.
      – cuonglm
      May 10 '14 at 15:55
















    • It appends name of the matched file before the output. Is there any way to suppress that? Except that the solutions is perfect.
      – user1263746
      May 10 '14 at 14:35










    • @user1263746 To do what you want cat *.txt | grep ...
      – Joseph R.
      May 10 '14 at 14:37










    • yes you can.grep -io 'rah' *.txt | awk -F: ' print $2'. This command removes the file-name from the output.
      – Avinash Raj
      May 10 '14 at 14:37







    • 1




      grep -ioh does the job :) Thanks guys!
      – user1263746
      May 10 '14 at 14:45










    • @AvinashRaj: This doesn't find the combinations of string as the OP want.
      – cuonglm
      May 10 '14 at 15:55















    It appends name of the matched file before the output. Is there any way to suppress that? Except that the solutions is perfect.
    – user1263746
    May 10 '14 at 14:35




    It appends name of the matched file before the output. Is there any way to suppress that? Except that the solutions is perfect.
    – user1263746
    May 10 '14 at 14:35












    @user1263746 To do what you want cat *.txt | grep ...
    – Joseph R.
    May 10 '14 at 14:37




    @user1263746 To do what you want cat *.txt | grep ...
    – Joseph R.
    May 10 '14 at 14:37












    yes you can.grep -io 'rah' *.txt | awk -F: ' print $2'. This command removes the file-name from the output.
    – Avinash Raj
    May 10 '14 at 14:37





    yes you can.grep -io 'rah' *.txt | awk -F: ' print $2'. This command removes the file-name from the output.
    – Avinash Raj
    May 10 '14 at 14:37





    1




    1




    grep -ioh does the job :) Thanks guys!
    – user1263746
    May 10 '14 at 14:45




    grep -ioh does the job :) Thanks guys!
    – user1263746
    May 10 '14 at 14:45












    @AvinashRaj: This doesn't find the combinations of string as the OP want.
    – cuonglm
    May 10 '14 at 15:55




    @AvinashRaj: This doesn't find the combinations of string as the OP want.
    – cuonglm
    May 10 '14 at 15:55












    up vote
    1
    down vote













    As a beginner in Bash scripting I was looking exactly for this, and based on the accepted answer above, I wrote the following Nautilus script, which I named "Search Text in Directory...". As this will be useful for me from time to time, I thought it might also be useful for others as well.



    #!/bin/bash
    # Nautilus Script to search text in selected folder
    # Determine the path
    if [ -e -n $1 ]; then
    obj="$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS"
    else
    base="`echo $NAUTILUS_SCRIPT_CURRENT_URI | cut -d'/' -f3- | sed 's/%20/ /g'`"
    obj="$base/$1##*/"
    fi
    # Determine the type and go
    if [ -f "$obj" ]; then
    /usr/bin/canberra-gtk-play --id="dialog-error" &
    zenity --error --title="Search Directory" --text "Sorry, selected item is not a folder."
    elif [ -d "$obj" ]; then
    cd "$obj"
    # Get text to search
    SearchText=$(zenity --entry --title="Search Directory" --text="For Text:" --width=250)
    if [ -z "$SearchText" ]; then
    notify-send "Search Directory" "Nothing entered; exiting..." -i gtk-dialog-info -t 500 -u normal &
    exit
    else
    if [ -f "/tmp/Search-Directory-Results.txt" ]; then
    rm "/tmp/Search-Directory-Results.txt"
    fi
    grep_menu()

    im="zenity --list --radiolist --title="Search Directory" --text="Please select one of the search options below:""
    im=$im" --column="☉" --column "Option" --column "Description" "
    im=$im"TRUE "case-sensitive" "Match only: Text" "
    im=$im"FALSE "case-insensitive" "Match: TEXT, text, Text..." "

    grep_option()

    choice=`echo $im
    grep_menu
    grep_option
    fi
    zenity --class=LIST --text-info
    --editable
    --title="Search Directory"
    --filename="/tmp/Search-Directory-Results.txt"
    fi
    exit 0





    share|improve this answer
























      up vote
      1
      down vote













      As a beginner in Bash scripting I was looking exactly for this, and based on the accepted answer above, I wrote the following Nautilus script, which I named "Search Text in Directory...". As this will be useful for me from time to time, I thought it might also be useful for others as well.



      #!/bin/bash
      # Nautilus Script to search text in selected folder
      # Determine the path
      if [ -e -n $1 ]; then
      obj="$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS"
      else
      base="`echo $NAUTILUS_SCRIPT_CURRENT_URI | cut -d'/' -f3- | sed 's/%20/ /g'`"
      obj="$base/$1##*/"
      fi
      # Determine the type and go
      if [ -f "$obj" ]; then
      /usr/bin/canberra-gtk-play --id="dialog-error" &
      zenity --error --title="Search Directory" --text "Sorry, selected item is not a folder."
      elif [ -d "$obj" ]; then
      cd "$obj"
      # Get text to search
      SearchText=$(zenity --entry --title="Search Directory" --text="For Text:" --width=250)
      if [ -z "$SearchText" ]; then
      notify-send "Search Directory" "Nothing entered; exiting..." -i gtk-dialog-info -t 500 -u normal &
      exit
      else
      if [ -f "/tmp/Search-Directory-Results.txt" ]; then
      rm "/tmp/Search-Directory-Results.txt"
      fi
      grep_menu()

      im="zenity --list --radiolist --title="Search Directory" --text="Please select one of the search options below:""
      im=$im" --column="☉" --column "Option" --column "Description" "
      im=$im"TRUE "case-sensitive" "Match only: Text" "
      im=$im"FALSE "case-insensitive" "Match: TEXT, text, Text..." "

      grep_option()

      choice=`echo $im
      grep_menu
      grep_option
      fi
      zenity --class=LIST --text-info
      --editable
      --title="Search Directory"
      --filename="/tmp/Search-Directory-Results.txt"
      fi
      exit 0





      share|improve this answer






















        up vote
        1
        down vote










        up vote
        1
        down vote









        As a beginner in Bash scripting I was looking exactly for this, and based on the accepted answer above, I wrote the following Nautilus script, which I named "Search Text in Directory...". As this will be useful for me from time to time, I thought it might also be useful for others as well.



        #!/bin/bash
        # Nautilus Script to search text in selected folder
        # Determine the path
        if [ -e -n $1 ]; then
        obj="$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS"
        else
        base="`echo $NAUTILUS_SCRIPT_CURRENT_URI | cut -d'/' -f3- | sed 's/%20/ /g'`"
        obj="$base/$1##*/"
        fi
        # Determine the type and go
        if [ -f "$obj" ]; then
        /usr/bin/canberra-gtk-play --id="dialog-error" &
        zenity --error --title="Search Directory" --text "Sorry, selected item is not a folder."
        elif [ -d "$obj" ]; then
        cd "$obj"
        # Get text to search
        SearchText=$(zenity --entry --title="Search Directory" --text="For Text:" --width=250)
        if [ -z "$SearchText" ]; then
        notify-send "Search Directory" "Nothing entered; exiting..." -i gtk-dialog-info -t 500 -u normal &
        exit
        else
        if [ -f "/tmp/Search-Directory-Results.txt" ]; then
        rm "/tmp/Search-Directory-Results.txt"
        fi
        grep_menu()

        im="zenity --list --radiolist --title="Search Directory" --text="Please select one of the search options below:""
        im=$im" --column="☉" --column "Option" --column "Description" "
        im=$im"TRUE "case-sensitive" "Match only: Text" "
        im=$im"FALSE "case-insensitive" "Match: TEXT, text, Text..." "

        grep_option()

        choice=`echo $im
        grep_menu
        grep_option
        fi
        zenity --class=LIST --text-info
        --editable
        --title="Search Directory"
        --filename="/tmp/Search-Directory-Results.txt"
        fi
        exit 0





        share|improve this answer












        As a beginner in Bash scripting I was looking exactly for this, and based on the accepted answer above, I wrote the following Nautilus script, which I named "Search Text in Directory...". As this will be useful for me from time to time, I thought it might also be useful for others as well.



        #!/bin/bash
        # Nautilus Script to search text in selected folder
        # Determine the path
        if [ -e -n $1 ]; then
        obj="$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS"
        else
        base="`echo $NAUTILUS_SCRIPT_CURRENT_URI | cut -d'/' -f3- | sed 's/%20/ /g'`"
        obj="$base/$1##*/"
        fi
        # Determine the type and go
        if [ -f "$obj" ]; then
        /usr/bin/canberra-gtk-play --id="dialog-error" &
        zenity --error --title="Search Directory" --text "Sorry, selected item is not a folder."
        elif [ -d "$obj" ]; then
        cd "$obj"
        # Get text to search
        SearchText=$(zenity --entry --title="Search Directory" --text="For Text:" --width=250)
        if [ -z "$SearchText" ]; then
        notify-send "Search Directory" "Nothing entered; exiting..." -i gtk-dialog-info -t 500 -u normal &
        exit
        else
        if [ -f "/tmp/Search-Directory-Results.txt" ]; then
        rm "/tmp/Search-Directory-Results.txt"
        fi
        grep_menu()

        im="zenity --list --radiolist --title="Search Directory" --text="Please select one of the search options below:""
        im=$im" --column="☉" --column "Option" --column "Description" "
        im=$im"TRUE "case-sensitive" "Match only: Text" "
        im=$im"FALSE "case-insensitive" "Match: TEXT, text, Text..." "

        grep_option()

        choice=`echo $im
        grep_menu
        grep_option
        fi
        zenity --class=LIST --text-info
        --editable
        --title="Search Directory"
        --filename="/tmp/Search-Directory-Results.txt"
        fi
        exit 0






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 20 '16 at 23:16









        Sadi

        220111




        220111



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f128792%2fgrep-find-all-possible-cases-of-a-word-in-text-file%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown






            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay