Grep : Find all possible cases of a word in text file
Clash 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.
grep
add a comment |
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.
grep
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-i
switch 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
add a comment |
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.
grep
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
grep
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-i
switch 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
add a comment |
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-i
switch 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
-i
switch for that.– Joseph R.
May 10 '14 at 14:36
What you want is called case-insensitive match. Use the
-i
switch 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
add a comment |
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
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 wantcat *.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
|
show 2 more comments
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
add a comment |
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
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 wantcat *.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
|
show 2 more comments
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
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 wantcat *.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
|
show 2 more comments
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
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
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 wantcat *.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
|
show 2 more comments
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 wantcat *.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
|
show 2 more comments
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
add a comment |
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
add a comment |
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
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
answered Dec 20 '16 at 23:16
Sadi
220111
220111
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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
-i
switch 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