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)

1
  • I also find man sudo | grep -n "\|$" | grep 15: very useful, it prints line 15 in the sudo manual page.
    – Shayan
    Feb 25, 2018 at 19:28

3 Answers 3

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
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.

2
  • 1
    grep -A5 -B5 is actually just grep -C5 ;-D --- You don't know how many lines you will need to get the whole paragraph though.
    – Byte Commander
    Feb 8, 2018 at 14:29
  • 1
    This is a grep solution because the OP used grep. But the less' search function is actually the proper solution ;-)
    – pLumo
    Feb 8, 2018 at 14:31
1

Maybe the most pager would satisfy your requirements. Try

export PAGER=most
man sudo
/-k
3
  • s@s-N ~ $ export PAGER=most then s@s-N ~ $ man sudo returns error man: can't execute most: No such file or directory
    – Shayan
    Feb 8, 2018 at 14:17
  • 1
    Install most from your favorite package manager. For example sudo aptitude install most.
    – S.M.
    Feb 8, 2018 at 14:19
  • 2
    There is no need to install anything. You can already search in man using `/´.
    – Soren A
    Feb 8, 2018 at 14:23

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