Score:2

How to search output of help message of program (using e.g. grep)

cn flag

I have a directory with executables and I want to search for a word in the output of the help messages of these executables, i.e. the text that is printed in the console after using the command ./executable1 --help. I want to output both the executable name and the occurrence of the searched text just like grep would do for a text file. How do I do this?


I managed something close with:

find -name "exec*" -executable -exec {} --help \; | grep "stringToBeSearchedFor" --

where find should have some criteria to find all the executables (in this example they all start with "exec" so this is appropriate).

However, this doesn't print the name of the executable with the match.

Score:3
hr flag

I would probably make use of grep's -H and --label options:

   --label=LABEL
          Display  input  actually  coming  from  standard  input as input
          coming from file LABEL.  This can be useful  for  commands  that
          transform  a  file's  contents  before searching, e.g., gzip -cd
          foo.gz | grep --label=foo -H 'some pattern'.  See  also  the  -H
          option.

Ex.

find . -type f -name 'exec*' -executable -exec sh -c '
  for f; do "$f" --help | grep -H --label="$f" -- "stringToBeSearchedFor"; done
' sh {} +

If the help messages may be printed to the standard error stream rather than the standard output stream, change "$f" --help | grep ... to "$f" --help 2>&1 | grep ...

Martin Thornton avatar
cn flag
This assumes the help messages will be output to `stdout` rather than `stderr`.
hr flag
@MartinThornton indeed it does - I assumed that was appropriate here based on the OP's statement that their version "comes close" but "doesn't print the name"
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.