How to find files containing set of words on linux [closed]
Clash Royale CLAN TAG#URR8PPP
up vote
-1
down vote
favorite
I have number of text files line fruit1, fruit2, fruit3, fruit4 and so on in one directory. Now I want to find the files which contains all three words "Appale" , "Banana" & "Orange".
All three must be present in single file.
linux aix
closed as unclear what you're asking by Jeff Schaller, Rui F Ribeiro, schily, msp9011, RalfFriedl Dec 8 at 17:05
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
up vote
-1
down vote
favorite
I have number of text files line fruit1, fruit2, fruit3, fruit4 and so on in one directory. Now I want to find the files which contains all three words "Appale" , "Banana" & "Orange".
All three must be present in single file.
linux aix
closed as unclear what you're asking by Jeff Schaller, Rui F Ribeiro, schily, msp9011, RalfFriedl Dec 8 at 17:05
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
1
What about Apple?
– Rui F Ribeiro
Dec 7 at 11:28
4
Why is the AIX tag included?
– Jeff Schaller
Dec 7 at 11:35
1
What have your tried and what errors do you encounter?
– kemotep
Dec 7 at 11:47
How to run grep with multiple AND patterns?
– steeldriver
Dec 7 at 13:40
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I have number of text files line fruit1, fruit2, fruit3, fruit4 and so on in one directory. Now I want to find the files which contains all three words "Appale" , "Banana" & "Orange".
All three must be present in single file.
linux aix
I have number of text files line fruit1, fruit2, fruit3, fruit4 and so on in one directory. Now I want to find the files which contains all three words "Appale" , "Banana" & "Orange".
All three must be present in single file.
linux aix
linux aix
asked Dec 7 at 11:26
Nitin H
6
6
closed as unclear what you're asking by Jeff Schaller, Rui F Ribeiro, schily, msp9011, RalfFriedl Dec 8 at 17:05
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as unclear what you're asking by Jeff Schaller, Rui F Ribeiro, schily, msp9011, RalfFriedl Dec 8 at 17:05
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
1
What about Apple?
– Rui F Ribeiro
Dec 7 at 11:28
4
Why is the AIX tag included?
– Jeff Schaller
Dec 7 at 11:35
1
What have your tried and what errors do you encounter?
– kemotep
Dec 7 at 11:47
How to run grep with multiple AND patterns?
– steeldriver
Dec 7 at 13:40
add a comment |
1
What about Apple?
– Rui F Ribeiro
Dec 7 at 11:28
4
Why is the AIX tag included?
– Jeff Schaller
Dec 7 at 11:35
1
What have your tried and what errors do you encounter?
– kemotep
Dec 7 at 11:47
How to run grep with multiple AND patterns?
– steeldriver
Dec 7 at 13:40
1
1
What about Apple?
– Rui F Ribeiro
Dec 7 at 11:28
What about Apple?
– Rui F Ribeiro
Dec 7 at 11:28
4
4
Why is the AIX tag included?
– Jeff Schaller
Dec 7 at 11:35
Why is the AIX tag included?
– Jeff Schaller
Dec 7 at 11:35
1
1
What have your tried and what errors do you encounter?
– kemotep
Dec 7 at 11:47
What have your tried and what errors do you encounter?
– kemotep
Dec 7 at 11:47
How to run grep with multiple AND patterns?
– steeldriver
Dec 7 at 13:40
How to run grep with multiple AND patterns?
– steeldriver
Dec 7 at 13:40
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
for filename in *; do
if [ -f "$filename" ] &&
grep -qF Appale -- "$filename" &&
grep -qF Banana -- "$filename" &&
grep -qF Orange -- "$filename"
then
printf '%s contains all three stringsn' "$filename"
fi
done
This would report any regular file (or symbolic link to a regular file) in the current directory that contains the all the given strings (not necessarily on the same line). If your grep
supports -w
, then you may use this as well to stop substrings from being found (such as the substring Banana
in the word Bananas
).
The -F
makes grep
do string matching rather than full regular expression matching, and the -q
makes it quiet (we are only interested in the exit status of the utility).
A solution that generalises the above to any set of words:
set -- Appale Banana Orange Bumblebee Botswana
for filename in *; do
if [ ! -f "$filename" ] && continue
found=1
for string do
if ! grep -qF -e "$string" -- "$filename"; then
found=0
break
fi
done
if [ "$found" -eq 1 ]; then
printf '%s contains all stringsn' "$filename"
fi
done
add a comment |
up vote
0
down vote
How about
grep -E "Appale|Banana|Orange" fi* | cut -d: -f1 | uniq -c | grep "^ *3"
3 file2
3 file3
Should patterns occur multiple times in a file, i.e. a count > 3, we need a different approach.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
for filename in *; do
if [ -f "$filename" ] &&
grep -qF Appale -- "$filename" &&
grep -qF Banana -- "$filename" &&
grep -qF Orange -- "$filename"
then
printf '%s contains all three stringsn' "$filename"
fi
done
This would report any regular file (or symbolic link to a regular file) in the current directory that contains the all the given strings (not necessarily on the same line). If your grep
supports -w
, then you may use this as well to stop substrings from being found (such as the substring Banana
in the word Bananas
).
The -F
makes grep
do string matching rather than full regular expression matching, and the -q
makes it quiet (we are only interested in the exit status of the utility).
A solution that generalises the above to any set of words:
set -- Appale Banana Orange Bumblebee Botswana
for filename in *; do
if [ ! -f "$filename" ] && continue
found=1
for string do
if ! grep -qF -e "$string" -- "$filename"; then
found=0
break
fi
done
if [ "$found" -eq 1 ]; then
printf '%s contains all stringsn' "$filename"
fi
done
add a comment |
up vote
1
down vote
for filename in *; do
if [ -f "$filename" ] &&
grep -qF Appale -- "$filename" &&
grep -qF Banana -- "$filename" &&
grep -qF Orange -- "$filename"
then
printf '%s contains all three stringsn' "$filename"
fi
done
This would report any regular file (or symbolic link to a regular file) in the current directory that contains the all the given strings (not necessarily on the same line). If your grep
supports -w
, then you may use this as well to stop substrings from being found (such as the substring Banana
in the word Bananas
).
The -F
makes grep
do string matching rather than full regular expression matching, and the -q
makes it quiet (we are only interested in the exit status of the utility).
A solution that generalises the above to any set of words:
set -- Appale Banana Orange Bumblebee Botswana
for filename in *; do
if [ ! -f "$filename" ] && continue
found=1
for string do
if ! grep -qF -e "$string" -- "$filename"; then
found=0
break
fi
done
if [ "$found" -eq 1 ]; then
printf '%s contains all stringsn' "$filename"
fi
done
add a comment |
up vote
1
down vote
up vote
1
down vote
for filename in *; do
if [ -f "$filename" ] &&
grep -qF Appale -- "$filename" &&
grep -qF Banana -- "$filename" &&
grep -qF Orange -- "$filename"
then
printf '%s contains all three stringsn' "$filename"
fi
done
This would report any regular file (or symbolic link to a regular file) in the current directory that contains the all the given strings (not necessarily on the same line). If your grep
supports -w
, then you may use this as well to stop substrings from being found (such as the substring Banana
in the word Bananas
).
The -F
makes grep
do string matching rather than full regular expression matching, and the -q
makes it quiet (we are only interested in the exit status of the utility).
A solution that generalises the above to any set of words:
set -- Appale Banana Orange Bumblebee Botswana
for filename in *; do
if [ ! -f "$filename" ] && continue
found=1
for string do
if ! grep -qF -e "$string" -- "$filename"; then
found=0
break
fi
done
if [ "$found" -eq 1 ]; then
printf '%s contains all stringsn' "$filename"
fi
done
for filename in *; do
if [ -f "$filename" ] &&
grep -qF Appale -- "$filename" &&
grep -qF Banana -- "$filename" &&
grep -qF Orange -- "$filename"
then
printf '%s contains all three stringsn' "$filename"
fi
done
This would report any regular file (or symbolic link to a regular file) in the current directory that contains the all the given strings (not necessarily on the same line). If your grep
supports -w
, then you may use this as well to stop substrings from being found (such as the substring Banana
in the word Bananas
).
The -F
makes grep
do string matching rather than full regular expression matching, and the -q
makes it quiet (we are only interested in the exit status of the utility).
A solution that generalises the above to any set of words:
set -- Appale Banana Orange Bumblebee Botswana
for filename in *; do
if [ ! -f "$filename" ] && continue
found=1
for string do
if ! grep -qF -e "$string" -- "$filename"; then
found=0
break
fi
done
if [ "$found" -eq 1 ]; then
printf '%s contains all stringsn' "$filename"
fi
done
edited Dec 7 at 13:10
answered Dec 7 at 12:58
Kusalananda
120k16225369
120k16225369
add a comment |
add a comment |
up vote
0
down vote
How about
grep -E "Appale|Banana|Orange" fi* | cut -d: -f1 | uniq -c | grep "^ *3"
3 file2
3 file3
Should patterns occur multiple times in a file, i.e. a count > 3, we need a different approach.
add a comment |
up vote
0
down vote
How about
grep -E "Appale|Banana|Orange" fi* | cut -d: -f1 | uniq -c | grep "^ *3"
3 file2
3 file3
Should patterns occur multiple times in a file, i.e. a count > 3, we need a different approach.
add a comment |
up vote
0
down vote
up vote
0
down vote
How about
grep -E "Appale|Banana|Orange" fi* | cut -d: -f1 | uniq -c | grep "^ *3"
3 file2
3 file3
Should patterns occur multiple times in a file, i.e. a count > 3, we need a different approach.
How about
grep -E "Appale|Banana|Orange" fi* | cut -d: -f1 | uniq -c | grep "^ *3"
3 file2
3 file3
Should patterns occur multiple times in a file, i.e. a count > 3, we need a different approach.
answered Dec 7 at 12:34
RudiC
3,9391312
3,9391312
add a comment |
add a comment |
1
What about Apple?
– Rui F Ribeiro
Dec 7 at 11:28
4
Why is the AIX tag included?
– Jeff Schaller
Dec 7 at 11:35
1
What have your tried and what errors do you encounter?
– kemotep
Dec 7 at 11:47
How to run grep with multiple AND patterns?
– steeldriver
Dec 7 at 13:40