Search for an exact string in a Terminal

Is there hopefully a way to search for an exact string in Man Pages? I know if I want to search for anything containing -c I can just do this.

/-c

How would I search for "-c"? I want only "-c" to show up. So I tried this.

/"-c"

It took me literally and looked for the quotes also.

did you try grep ???

You can use ^ for beginning of line, but apparently not $, at least not on my ksh.

/^-c

You can grep in the shell history, but there are control characters in there, so look at it with "cat -vte" or "od -bc" first.

I forgot to say I'm talking about in the man pages. When you go to the help pages it says something similar to this.

Man pages are displayed on $PAGER, so you get your choice. The default is more or less. I use pg when i can get it. However, pager regex rules are a bit kinky.

Beyond what the more or less man page says about pattern searching and moving forward and, if you have a file input, backward:

The man pages have teletype print artifacts in them like "_\ba" for underscored "a" and "c\bc" for bold "c" (\b is backspace to overstrike). You can write a sed routine to flush out the .\b or you can use a web man page.

sed '
        s/\(.\)\^H\1/\1/g
        s/\(.\)\^H\1/\1/g
        s/\(.\)\^H\1/\1/g
        s/\(.\)\^H_/\1/g
        s/_\^H\(.\)/\1/g
        s/.\^H\(.\)/\1/g
 '|sed '
        /^$/{
                :l
                $b
                N
                s/^\n$//
                t l
                }
 '

or in C:

$ cat mysrc/manclean.c

#include <stdio.h>

static void p_putchar( int c )
{
        if ( c != EOF
          && EOF == putchar( c ) )
        {
                if ( ferror( stdout ) )
                {
                        perror( "stdout" );
                        exit( 1 );
                }

                exit( 0 );
        }
}

main()
{
        static int c ;
        static int c2 ;
        static int c3 ;
        static int ct = 0 ;

        do
        {
                switch( c = getchar() )
                {
                case EOF:
                        if ( ferror( stdin ) )
                        {
                                perror( "stdin" );
                        }
                        continue ;

                case '\b':
                        if ( ct < 2 )
                        {
                                ct = 0 ;
                                continue ;
                        }

                        c2 = c3 ;
                        ct = 1 ;
                        continue ;

                case '\n':
                        if ( ct > 1
                          && c3 == '\n'
                          && c2 == '\n' )
                        {
                                ct = 2 ;
                                continue ;
                        }

                        /* intentional fall through */
                default:
                        break ;
                }

                switch( ++ct )
                {
                case 1:
                        c2 = c ;
                        continue ;

                case 2:
                        break ;
                default:
                        p_putchar( c3 );
                        break ;
                }

                c3 = c2 ;
                c2 = c ;

        } while ( c != EOF );

        switch( ct )
        {
        case 0:
                break ;
        case 1:
                p_putchar( c2 );
        default:
                p_putchar( c3 );
                p_putchar( c2 );
                break ;
        }

        exit( 0 );
}

you can use \b

when viewing man, type any of these

/\b-c\b
/\b-c
/-c\b

What does the \b do? It doesn't seem like it is doing anything.

Someone figured this out for me and it works.

"/ -c "

(without the quotes but notice the space before and after the -c)

This also does the trick.

/[^a-z]-c[^a-z]

i used to use

man any_ninja_command | less

since less highlight the pattern you search for, which is much more confortable

by the way, if

/-c

didn't work, did you try

/\-c

?

\b is backspace, part of bold or underline for teletype printers in nroff/troff, which is the "word processor" for man pages.

1 Like

I'll sleep a bit less ignorant tonight, tya DGnitPick:)

I think in this case it stands for "word boundary" not backspace.

1 Like

Oki dude, thanx !

My bad:

Regex Tutorial - \b Word Boundaries

For me, the pattern '-..c' works, because of the overtyping boldness encoded by nroff/troff in the man page, see man grep example below:

$ man grep             



 grep(1)                                                             grep(1)




 NAME
      grep, egrep, fgrep - search a file for a pattern

 SYNOPSIS
    Plain call with pattern
      grep [-E|-F] [-c|-l|-q] [-bhinsvx] pattern [file ...]
 .
 .
 .
           -c                  Only a count of matching lines is printed.
 .
 .
 .
 .



===========================================================
$ man grep | cat -v



 g^Hg^Hg^Hgr^Hr^Hr^Hre^He^He^Hep^Hp^Hp^Hp(^H(^H(^H(1^H1^H1^H1)^H)^H)^H)             g^Hg^Hg^Hgr^Hr^Hr^Hre^He^He^Hep^Hp^Hp^Hp(^H(^H(^H(1^H1^H1^H1)^H)^H)^H)




 N^HN^HN^HNA^HA^HA^HAM^HM^HM^HME^HE^HE^HE
      grep, egrep, fgrep - search a file for a pattern

 S^HS^HS^HSY^HY^HY^HYN^HN^HN^HNO^HO^HO^HOP^HP^HP^HPS^HS^HS^HSI^HI^HI^HIS^HS^HS^HS
    P^HP^HP^HPl^Hl^Hl^Hla^Ha^Ha^Hai^Hi^Hi^Hin^Hn^Hn^Hn c^Hc^Hc^Hca^Ha^Ha^Hal^Hl^Hl^Hll^Hl^Hl^Hl w^Hw^Hw^Hwi^Hi^Hi^Hit^Ht^Ht^Hth^Hh^Hh^Hh p^Hp^Hp^Hpa^Ha^Ha^Hat^Ht^Ht^Htt^Ht^Ht^Hte^He^He^Her^Hr^Hr^Hrn^Hn^Hn^Hn
      g^Hg^Hg^Hgr^Hr^Hr^Hre^He^He^Hep^Hp^Hp^Hp [-^H-^H-^H-E^HE^HE^HE|-^H-^H-^H-F^HF^HF^HF] [-^H-^H-^H-c^Hc^Hc^Hc|-^H-^H-^H-l^Hl^Hl^Hl|-^H-^H-^H-q^Hq^Hq^Hq] [-^H-^H-^H-b^Hb^Hb^Hbh^Hh^Hh^Hhi^Hi^Hi^Hin^Hn^Hn^Hns^Hs^Hs^Hsv^Hv^Hv^Hvx^Hx^Hx^Hx] _^Hp_^Ha_^Ht_^Ht_^He_^Hr_^Hn [_^Hf_^Hi_^Hl_^He ...]
 .
 .
 .
           -^H-^H-^H-c^Hc^Hc^Hc                Only a count of matching lines is printed.
 .
 .
 .
 .

But it also works if you just use less on a regular plain text file and use word boundaries.