user213544
user213544

Reputation: 2126

How to find text files with nothing but a pattern on a single line using bash

I am trying to find text files using bash that contain nothing but a specific pattern on 1 line of the file.

For example, I have the following textfile:

1234123 123412341 0000 23423 23422
121231 123123 12312 12312 1231
567 567 43 234 12
0000
929 020 040 040 0000

This file contains a line (line 4), that exclusively has pattern 0000. I tried ls | grep 0000, however, that returns also the files in which the pattern is located elsewhere in the file and not necessarily 'solo' on a line.

How do you find a pattern using bash that is exclusively present on a single line of the file?

Upvotes: 0

Views: 42

Answers (1)

Benjamin W.
Benjamin W.

Reputation: 52102

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

Upvotes: 1

Related Questions