Issue
I need to search for files that contain 2 or more occurrences of a specific word (in my case NORMAL
), so from files like the following:
file1.txt:
the NORMAL things are [
- case
- case 2
a NORMAL is like [
- case 3
- case 4
]
]
file2.txt:
the NORMAL things are [
- case
- case 2
a DIFFERENT is like [
- case 3
- case 4
]
]
file3.txt:
the NORMAL things are [
- case
- case 2
]
it will find file1.txt only.
I have tried with a simple grep
:
grep -Ri "*NORMAL*NORMAL*" .
but it does not work.
Thanks
Solution
If you do not wish to search recursively:
grep -lzE '(NORMAL).*\1' files*
If you do wish to search recursively:
grep -rlzE '(NORMAL).*\1' .
This command is checking recursively in the current directory, for the file which contains NORMAL
followed by NORMAL
(\1
) in the file. Meaning it will match 2 or more matches. This is only printing filename, remove -l
to print the content + filename.
-l
: This would only print the file name if matched by grep
-z
: a data line ends in 0 byte, not a newline
-E
: use extended regular expression
-r
: recursive
Answered By - P....