1

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.

4
  • 2
    Have you tried the man page?
    – Joseph R.
    May 10, 2014 at 14:31
  • Tried, failed. Maybe too dumb to make sense of man pages. May 10, 2014 at 14:32
  • 1
    What you want is called case-insensitive match. Use the -iswitch for that.
    – Joseph R.
    May 10, 2014 at 14:36
  • @JosephR. It will print while whole line, I just need the matched word. See Avinash's answer below. May 10, 2014 at 14:44

2 Answers 2

7

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
7
  • It appends name of the matched file before the output. Is there any way to suppress that? Except that the solutions is perfect. May 10, 2014 at 14:35
  • @user1263746 To do what you want cat *.txt | grep ...
    – Joseph R.
    May 10, 2014 at 14:37
  • yes you can.grep -io 'rah' *.txt | awk -F: '{ print $2}'. This command removes the file-name from the output. May 10, 2014 at 14:37
  • 1
    grep -ioh does the job :) Thanks guys! May 10, 2014 at 14:45
  • @AvinashRaj: This doesn't find the combinations of string as the OP want.
    – cuonglm
    May 10, 2014 at 15:55
1

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 | sh -`
        if echo $choice | grep -iE "case-sensitive|case-insensitive" > /dev/null
        then
            if echo $choice | grep "case-sensitive" > /dev/null
            then
                grep -- "$SearchText" *.* > "/tmp/Search-Directory-Results.txt"
            fi
            if echo $choice | grep "case-insensitive" > /dev/null
            then
                grep -i -- "$SearchText" *.* > "/tmp/Search-Directory-Results.txt"
            fi
        fi
        }
        grep_menu
        grep_option
    fi
    zenity  --class=LIST --text-info \
            --editable \
            --title="Search Directory" \
            --filename="/tmp/Search-Directory-Results.txt"
fi
exit 0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .