How to list all files that do not contain two different lines

List of all files that do not contain 2 different lines

I have a dir with numerous files named in the template, for example file1.txt

I can list all files that do not contain one line

grep -l "String" file* 

How can I list files that do not contain two lines that I tried?

 grep -l "string1|string2" file* 
+4
source share
2 answers

You need the e parameter for grep or with egrep.

With egrep:

 egrep -L "string1|string2" file* 
+4
source

Assuming you just want to print the names of files containing ALL strings, here is a solution that will work for any number of strings and will do string comparisons, rather than regular expression comparisons:

 gawk -v RS='\0' -v strings="string1 string2" ' BEGIN{ numStrings = split(strings,stringsA) } { matchCnt = 0 for (stringNr=1; stringNr<=numStrings; stringNr++) if ( index($0,stringsA[stringNr]) ) matchCnt++ } matchCnt == numStrings { print FILENAME } ' file* 

Wait, I just noticed that you want to print files that DO NOT contain 2 lines. It will be:

 gawk -v RS='\0' -v strings="string1 string2" ' BEGIN{ numStrings = split(strings,stringsA) } { matchCnt = 0 for (stringNr=1; stringNr<=numStrings; stringNr++) if ( index($0,stringsA[stringNr]) ) matchCnt++ } matchCnt == numStrings { matchesAll[FILENAME] } END { for (fileNr=1; fileNr < ARGC; fileNr++) { file = ARGV[fileNr] if (! (file in matchesAll) ) print file } } ' file* 

To print the names of files that do not contain a single line, follow these steps:

 gawk -v RS='\0' -v strings="string1 string2" ' BEGIN{ numStrings = split(strings,stringsA) } { for (stringNr=1; stringNr<=numStrings; stringNr++) if ( index($0,stringsA[stringNr]) ) matchesOne[FILENAME] } END { for (fileNr=1; fileNr < ARGC; fileNr++) { file = ARGV[fileNr] if (! (file in matchesOne) ) print file } } ' file* 
+1
source

All Articles