Skip to main content
added 148 characters in body
Source Link
Benjamin W.
  • 54k
  • 19
  • 135
  • 136

Assuming we have four input files:

$ head file* ==> file1 <== 0000 0000 ==> file2 <== abcd 0000 abcd ==> file3 <== 0000x ==> file4 <== abcd 

file4 doesn't contain the pattern at all, file3 contains the pattern, but it's not on a line on its own, file1 has multiple lines that contain just the pattern, and file2 has exactly one line with just the pattern.

To get all files that contain the pattern anywhere:

$ grep -l '0000' file* file1 file2 file3 

To get all files that contain lines with nothing but the pattern:

$ grep -lx '0000' file* file1 file2 

And if you wanted only files that contain exactly one line with nothing but the pattern, you could use -c to get a count first:

$ grep -xc '0000' file* file1:2 file2:1 file3:0 file4:0 

and then use awk to print only the files with exactly one match:

$ grep -xc '0000' file* | awk -F: '$2==1 {print $1}' file2 

With GNU awk, you could also do this directly:

$ awk 'BEGINFILE {c=0} /^0000$/ {++c} ENDFILE {if (c==1) print FILENAME}' file* file2 

Assuming we have four input files:

$ head file* ==> file1 <== 0000 0000 ==> file2 <== abcd 0000 abcd ==> file3 <== 0000x ==> file4 <== abcd 

file4 doesn't contain the pattern at all, file3 contains the pattern, but it's not on a line on its own, file1 has multiple lines that contain just the pattern, and file2 has exactly one line with just the pattern.

To get all files that contain the pattern anywhere:

$ grep -l '0000' file* file1 file2 file3 

To get all files that contain lines with nothing but the pattern:

$ grep -lx '0000' file* file1 file2 

And if you wanted only files that contain exactly one line with nothing but the pattern, you could use -c to get a count first:

$ grep -xc '0000' file* file1:2 file2:1 file3:0 file4:0 

and then use awk to print only the files with exactly one match:

$ grep -xc '0000' file* | awk -F: '$2==1 {print $1}' file2 

Assuming we have four input files:

$ head file* ==> file1 <== 0000 0000 ==> file2 <== abcd 0000 abcd ==> file3 <== 0000x ==> file4 <== abcd 

file4 doesn't contain the pattern at all, file3 contains the pattern, but it's not on a line on its own, file1 has multiple lines that contain just the pattern, and file2 has exactly one line with just the pattern.

To get all files that contain the pattern anywhere:

$ grep -l '0000' file* file1 file2 file3 

To get all files that contain lines with nothing but the pattern:

$ grep -lx '0000' file* file1 file2 

And if you wanted only files that contain exactly one line with nothing but the pattern, you could use -c to get a count first:

$ grep -xc '0000' file* file1:2 file2:1 file3:0 file4:0 

and then use awk to print only the files with exactly one match:

$ grep -xc '0000' file* | awk -F: '$2==1 {print $1}' file2 

With GNU awk, you could also do this directly:

$ awk 'BEGINFILE {c=0} /^0000$/ {++c} ENDFILE {if (c==1) print FILENAME}' file* file2 
Source Link
Benjamin W.
  • 54k
  • 19
  • 135
  • 136

Assuming we have four input files:

$ head file* ==> file1 <== 0000 0000 ==> file2 <== abcd 0000 abcd ==> file3 <== 0000x ==> file4 <== abcd 

file4 doesn't contain the pattern at all, file3 contains the pattern, but it's not on a line on its own, file1 has multiple lines that contain just the pattern, and file2 has exactly one line with just the pattern.

To get all files that contain the pattern anywhere:

$ grep -l '0000' file* file1 file2 file3 

To get all files that contain lines with nothing but the pattern:

$ grep -lx '0000' file* file1 file2 

And if you wanted only files that contain exactly one line with nothing but the pattern, you could use -c to get a count first:

$ grep -xc '0000' file* file1:2 file2:1 file3:0 file4:0 

and then use awk to print only the files with exactly one match:

$ grep -xc '0000' file* | awk -F: '$2==1 {print $1}' file2