Reputation: 25
I am trying to search for files with specific text but excluding a certain text and showing only the files.
Here is my code:
grep -v "TEXT1" *.* | grep -ils "ABC2"
However, it returns:
(standard input)
Please suggest. Thanks a lot.
The output should only show the filenames.
Upvotes: 1
Views: 1311
Reputation: 23667
Here's one way to do it, assuming you want to match these terms anywhere in the file.
grep -LZ 'TEXT1' *.* | xargs -0 grep -li 'ABC2'
-L
will match files not containing the given search term
-LiZ
if you want to match TEXT1
irrespective of case-Z
option is needed to separate filenames with NUL character and xargs -0
will then separate out filenames based on NUL characterIf you want to check these two conditions on same line instead of anywhere in the file:
grep -lP '^(?!.*TEXT1).*(?i:ABC2)' *.*
-P
enables PCRE, which I assume you have since linux
is tagged(?!regexp)
is a negative lookahead construct, so ^(?!.*TEXT1)
will ensure the line doesn't have TEXT1
(?i:ABC2)
will match ABC2
case insensitivelygrep -liP '^(?!.*TEXT1).*ABC2'
if you want to match both terms irrespective of caseUpvotes: 2
Reputation: 785146
(standard input)
This error is due to use of grep -l
in a pipeline as your second grep command is reading input from stdin not from a file and -l
option is printing (standard input)
instead of the filename.
You can use this alternate solution in a single awk
command:
awk '/ABC2/ && !/TEXT1/ {print FILENAME; nextfile}' *.* 2>/dev/null
Upvotes: 1