For me, it happens a lot when I want to search for an specific option in man page. I know that options are at the beginning of lines, but don't know how to limit the search.
I've tried /^REG-PATT
, but it didn't work for me.
What is the shortest correct pattern that I can use ?
Asked
Active
Viewed 468 times
1
-
I think the full answer to this question is _depends on your system_. `less` is sometimes used for `man` paging, and `less` can be compiled with different regex engines. – pb2q Aug 12 '12 at 18:52
-
I don't think so. It's about searching patterns in `less`. how so ? – Rsh Aug 12 '12 at 18:54
-
from `man less`: _The pattern is a regular expression, as recognized by the regular expression library supplied by your system_. Furthermore your question doesn't specifically mention `less` at all – pb2q Aug 12 '12 at 19:07
-
Thanks for clarification. In that case, I'm using Ubuntu. – Rsh Aug 12 '12 at 20:00
3 Answers
2
There's some space before the options. Maybe ^\s*-o
(-o
is the option you are searching) works. Or you can simply search the option in the whole line (-o
).

Lord Spectre
- 761
- 1
- 6
- 21
-
Yeah, that't working, thanks. But it's a little bit awkward, Do you know any other shortcuts instead of typing this every time ? – Rsh Aug 12 '12 at 18:35
-
3I just use `^ *foo` since I haven't ever seen a tab character in a rendered man page. And also... what the... `less` does perl regexps now? Are there any POSIX EREs left in the world? Do people even know that `\s` is non-standard? – Alan Curry Aug 12 '12 at 18:46
-
@AlanCurry I think `^ *-foo` is the closest answer. `^\s*` works on the linux machines that I tried, but not, e.g. on OSX. – pb2q Aug 12 '12 at 19:20
-
@pb2q It depends on the regexp engine used. For example, I tried it with Python's re module and it worked, but it didn't with GNU grep. – Lord Spectre Aug 12 '12 at 19:39
-
1That's my point, the answer to this depends on your system: which pager/regex engine. – pb2q Aug 12 '12 at 19:43
0
grep
it out with context, e.g. to list what -x
switch of tar
does:
man tar | grep -C5 -- "-x\b"
Edit
For getting documentation on -static
from gcc(1)
, you could do something like this with GNU sed:
man gcc | sed -n '/^ *-static/,/^ *-.*/p'
Note that the last line is from the next paragraph.
Here's a more elaborate and precise solution using GNU awk:
man gcc | gawk -v RS='\n\n' -v ORS='\n\n' '
/(^|\n) *-static/ { state = "printing" ; print "--" }
!/(^|\n) *-static/ { state = "not printing" }
state == "printing"
'

Thor
- 45,082
- 11
- 119
- 130
-
Try it with something like `-static` for gcc. Not so much helpful. BTW, tricky solution. – Rsh Aug 12 '12 at 18:52
-