Saturday, May 18, 2024
8
rated 0 times [  8] [ 0]  / answers: 1 / hits: 1204  / 2 Years ago, sat, march 26, 2022, 4:05:59

I want to search for specific options like -s, -f, -l in a manpage and display only the results that contain info on what those options do. I tried this command hoping that the single quotes would bypass grep from receiving options:


man --pager=cat some_man_page_here | grep '-option_here'

I also tried but grep gives me a syntax error:


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

I just wanna know if there's a way to use grep to search for options in manpages. I'm currently using the Find button on the terminal top bar but I'd like to be able to use grep for efficiency.


More From » command-line

 Answers
0

Would this work in your case?


$ man ls | grep -- '--a'
-a, --all
-A, --almost-all
--author

A more detailed (hopefully clearer) example of the command:


$ man shutdown | grep -- '-'
shutdown - Halt, power-off or reboot the machine
shutdown may be used to halt, power-off or reboot the machine.
logged-in users before going down.
--help
-H, --halt
-P, --poweroff
Power-off the machine (the default).
-r, --reboot
-h
Equivalent to --poweroff, unless --halt is specified.
-k
Do not halt, power-off, reboot, just write wall message.
--no-wall
Do not send wall message before halt, power-off, reboot.
-c
On success, 0 is returned, a non-zero failure code otherwise.

Edit:


As glenn jackman commented below (very useful):



And to narrow down the results to just lines starting with a hyphen:


grep '^[[:space:]]*-' – 


Test run:


$ man shutdown | grep -- '-' | grep '^[[:space:]]*-'
--help
-H, --halt
-P, --poweroff
-r, --reboot
-h
-k

One could obviously also use apropos in Linux.


[+] External Links:


Full-text search for man pages - At Unix SE


[#2581] Sunday, March 27, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
oargrou

Total Points: 336
Total Questions: 105
Total Answers: 113

Location: Vietnam
Member since Sun, Oct 18, 2020
4 Years ago
;