I have a text input like this:

ERROR=this is an error

The above two line will not occur at the same time. i.e. either TOKEN or ERROR but not both.

I use grep on the above input like this:

cat input |grep TOKEN

If there are error, the output will be nothing. What I want is that no matter what the text is, grep only match for the regex but not filter the text, if match is not found grep simply returns an non-zero exit code.

Is that possible, or any other command line tool to do this?

EDIT: I want the output of grep to be ALL input text, including non-matching linese, not NONE text, which the -q option of grep gives.


2 Answers 2


Sounds like a job for ack (using pass-through mode):

ack --passthru TOKEN input

or, depending on your distro

ack-grep --passthru --color TOKEN input

will print the whole input, highlight the matches and exit 1 if no match is found (and 0 if found).



grep -q TOKEN file && cat file       

The -q of grep is quiet mode. If the pattern is found, cat outputs the full contents of the file.

  • This is NOT what I want. I want the grep command to NOT filter text. i.e. whatever text it go through, it should output ALL text, including those not matching!
    – xrfang
    Commented Dec 29, 2022 at 0:42
  • @xrfang Consider the updated answer.
    – Kusalananda
    Commented Dec 29, 2022 at 6:20
  • @Kusalanda this does not work, or is awkward. becasue I am actually piping a serie of commands and using the jq tool to parse json output, blah blah...
    – xrfang
    Commented Dec 29, 2022 at 6:43
  • @xrfang If you want to clarify the question, then do so by editing the question. Note that most of the parsing, including tho test that grep or ack would be carrying out, can most likely be done by jq too, but you never show any JSON document... Also note that I was not notified of your comment due to you spelling my name wrong (I saw it by chance).
    – Kusalananda
    Commented Dec 29, 2022 at 7:48
  • @Kusalananda sorry for the typo. I do not want to explain my usage of jq, which might be suitable for another question. I may well use tee like this: command|tee /dev/tty|grep -q TOKEN, but anyway I just want to know what I asked in the question. thanks.
    – xrfang
    Commented Dec 29, 2022 at 8:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .