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.

Elder Geek
  • 36,023
  • 25
  • 98
  • 183

2 Answers2

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

Elder Geek
  • 36,023
  • 25
  • 98
  • 183
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
Yaron
  • 13,173