Grep keep (output) entire .txt file if one line contains string

cygwin;filesgreptext processing

I know that

grep -rhI "# Active" > out.txt 

will output any line containing # Active within the searched directory but I want the entire .txt file contents, so example

example.txt

Line1 
Line2
Line3 # Active
Line4
Line5
etc

So if I grep for # Active I want it to not just output the line containing # Active within these .txt file but all the other lines too, example

output.txt

Line1 
Line2
Line3 # Active
Line4
Line5
etc

Best Answer

For non-GNU versions of grep, which are unlikely to have -z, or if portability is required...

grep -q pattern file && cat file

-q suppresses any output but, per usual, exit status is set based on whether or not a pattern match was found. With a pattern match grep returns the success code 0 which is equivalent to true and that allows the cat command to be executed.

Related Question