1

Refer to can-i-get-individual-man-pages-for-the-bash-builtin-commands:

bashman () { man bash | less -p "^       $1 "; }

This function will jump to the desired bash manual parameter section directly.

I want to make it accept any Manual Name and Manual Parameter on the fly, so i change this function to:

function superman () {
    man "$1" | less -p "^[ ]+[-]*$2[ ]"
}

It works perfectly with:

$ superman bash type

And jump to the desired man page section:

enter image description here

But it doesn't work in section ending with newline. I need change it to:

function superman () {
    man "$1" | less -p "^[ ]+[-]*$2$"
}

Then run $ superman gcc Wall will able jump to Wall parameter:

enter image description here

How to combine [ ] and $ to be [ ] OR $? i.e. endswith at "atleast one space" OR "newline".

I tried $ man gcc | less -p "^[ ]+[-]*Wall[ |$]" but it doesn't works.

Please note that i accept for this undesired jump, which is endswith [ ] but not really a beginning of section, because i believe it's unsolvable if regex is [ ] OR $:

enter image description here

[Thanks for answers]

I want to share my final form here:

function superman () {
    if [[ "$1" == "-I" || "$1" == "-i" ]]; then
        man "$2" | less -I -p "^[ ]+-*$3( |=|,|$|\[)"
    else
        man "$1" | less -p "^[ ]+-*$2( |=|,|$|\[)"
    fi
}

This will work if i run superman -i gcc wl which means case-insensitive to jump into -Wl,option section. -Wl,option from gcc require ,. scaletempo from mplayer require [, and scale from mplayer require =

I wrote for info version too:

function superinfo () {
    if [[ "$1" == "-I" || "$1" == "-i" ]]; then
        info "$2" | less -I -p "^ *‘*'*-*\** *$3’*( |=|,|$|\[|:|')"
    else
        info "$1" | less -p "^ *‘*'*-*\** *$2’*( |=|,|$|\[|:|')"
    fi
}

Tested with superinfo gcc _HPPA, superinfo -i gcc werror, superinfo -i gcc wl, superinfo -i mplayer scaletempo, superinfo -i mplayer stats, superinfo -i ls -f, and superinfo -i bash -a(with minus sign make big difference here). Unicode's LEFT SINGLE QUOTATION MARK used by -f of ls.

林果皞
  • 5,156
  • 3
  • 33
  • 46

2 Answers2

1

How to combine [ ] and $ ?

([ ]|$) or ( |$)

sourcejedi
  • 50,249
1

I believe you are mistaken about the meaning of [ square brackets ] in a regular expression. Note your pattern:

^[ ]+[-]*Wall[ |$]

[ ] is just exactly the same as (a single space) and [-] is just exactly the same as -. And in the final part of the pattern, [foo|bar] does not signify "either foo or bar".

What you're looking for is ( |$). That's the syntax for matching one thing OR another thing. (You'll also need to quote that part of the pattern with single quotes or a backslash instead of double quotes, because of the dollar sign).

[thing] denotes a character class: it matches either t, h, i, n, or g.

Celada
  • 44,132
  • Thanks. But what do you means by quote that part of the pattern with single quotes or a backslash ? I put man "$1" | less -p "^[ ]+[-]*$2( |$)" and works fine, neither have single quotes nor backslash. – 林果皞 Oct 22 '16 at 18:46
  • 1
    You have a literal $ in there, so it should be quoted. I guess it "happens" to work by luck in this case because the $ is followed by something that cannot refer to a variable name. – Celada Oct 23 '16 at 06:07