0

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?

4
  • ls | grep 0000 doesn't look at file contents at all, it matches file names that contain 0000. Commented May 24, 2019 at 14:42
  • Hmmm, I think the duplicate might not be correct. Should it match only if there is exactly one line that matches the pattern? Commented May 24, 2019 at 14:43
  • What's the desired output? The names of the files containing exactly one matching line? Commented May 24, 2019 at 14:44
  • It should match, if there is at least one line that matches the pattern. Yes, the names are the desired output. Commented May 24, 2019 at 14:44

1 Answer 1

1

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 
Sign up to request clarification or add additional context in comments.

2 Comments

Is there a possibility to remove the line with the 0000 pattern from the files that contain this pattern? I tried using Awk, however, without succes.
@user213544 I'm not sure I understand; in general, to remove lines matching a certain pattern, you could use sed: sed -i '/0000/d' infile to remove lines containing 0000, and sed -i '/^0000$/d' infile to remove lines containing nothing but 0000. The -i option without a parameter requires GNU sed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.