0

I'm seeking the man page info for a switch for a given command.

I tried for example man sort | grep -A2 "-n,". I expected something like:

-n, --numeric-sort

    compare according to string numerical value

as output and instead I got

Usage: grep [OPTION]... PATTERN [FILE]... Try 'grep --help' for more information.

marked as duplicate by muru, Elder Geek, Community Oct 16 '17 at 14:51

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2

It turns out that -e is the required switch for grep to protect a pattern beginning with a hyphen (-) so to get the expected result the command would be: man sort | grep -A2 -e "-n,"

which results in:

-n, --numeric-sort
              compare according to string numerical value

--
              sort according to WORD: general-numeric -g, human-numeric -h, month -M, numeric -n, random -R, version -V

       -V, --version-sort

And if I want only the first 2 lines of matching output providing exactly what I expected I have to further pipe it through head as in man sort | grep -A2 -e "-n," | head -2

  • or grep -A2 -- "-n," which is telling grep there is no option later – αғsнιη Oct 16 '17 at 14:55
  • no. See man grep – Elder Geek Oct 16 '17 at 15:08
1

You can also escape the -n using \

man sort | grep -A2 "\-n,"

Output:

      -n, --numeric-sort
              compare according to string numerical value

--
              month -M, numeric -n, random -R, version -V

       -V, --version-sort

Not the answer you're looking for? Browse other questions tagged or ask your own question.