0

In the manual of sudo I need to search for -k so I tried this:

man sudo | grep '\-k'

But it would result a paragraph be half-written on the output:

sudo -h | -K | -k | -V
             Similar to the -k option, except that it removes the user's
 -k, --reset-timestamp

Instead I tried printing the whole manual and color the search phrase with:

man sudo | grep '\-k\|$'

But since the manual page is too long, I can't scroll up enough to reach the beginning of the output (Note: I don't want to change the Scrollback lines in the profile settings of Terminal).

So I need to use more or less commands, to be able to reveal the output by page, so I tried:

man sudo | grep '\-k\|$' | less

But of course it doesn't work, because pipe runs the 2nd command on the 1st command.

Any idea how to redirect pipe?

Or make grep print whole paragraph instead of one line? (the lines are based on Terminal's window size)

Shayan
  • 1,621

3 Answers3

3

man by default already uses less as a pager to display the manual page content.

Now less already has a built-in search and highlight feature, so you don't need an external tool like grep for that.

Simply open the manpage by running man sudo in a shell, then type /-k (press / and type your search pattern). less will now highlight wherever this regular expression matches in the document and also jump directly to the first occurrence.

You can then navigate to the next match by typing n (press N) or the previous match by typing N (press Shift+N).


More general, if you want to view grep's output with colorful highlighting in less, you must use grep --color=always (to always emit color codes, even when not outputting to a terminal but a pipe) and less -R (to interpret color escapes instead of printing the control characters):

man sudo | grep --color=always '\-k\|$' | less -R
pLumo
  • 27,991
Byte Commander
  • 110,243
3

Use grep's -A, -B and -C switches to catch the surrounding lines

man grep | grep '\-[ABC]' -A2

   -A NUM, --after-context=NUM
          Print NUM lines of trailing context after matching lines.  Places a line containing a group separator (described under --group-separator) between contiguous groups of matches.  With the -o or
          --only-matching option, this has no effect and a warning is given.

   -B NUM, --before-context=NUM
          Print NUM lines of leading context before matching lines.  Places a line containing a group separator (described under --group-separator) between contiguous groups of matches.  With the -o or
          --only-matching option, this has no effect and a warning is given.

   -C NUM, -NUM, --context=NUM
          Print  NUM  lines  of  output  context.   Places  a line containing a group separator (described under --group-separator) between contiguous groups of matches.  With the -o or --only-matching
          option, this has no effect and a warning is given.

In your case:

man sudo | grep '\-k' -A5 -B2

This is of course not perfect as you don't know how many lines are needed, but it may still help you find what you need.

pLumo
  • 27,991
1

Maybe the most pager would satisfy your requirements. Try

export PAGER=most
man sudo
/-k
3CEZVQ
  • 168