Search code examples
gccgrepmanpage

How does grep uses beginning and ending of lines


I want to read about one option in man page of gcc. So i did man gcc | grep -- '^-c'. The error was troff - cant break line, because I have terminal on the half on my screen. But I fixed that with declaration of MANWIDTH=1000 man gcc | grep -- '^-c' (thanks to: Why is this "can't break line" warning from grep of gcc man page?. But unfortunately it wont find it that option. Without the begginig pattern (that is only -c : in full : MANWIDTH=1000 man gcc | grep -- '-c', It will find all the occurrence of -c, which is quit a lot, but particularly the option I am looking for will be messed up with useless searches. So why does not the grep uses ^ and $ as beginning and end of the line? Or does it have something to do with the size of my terminal window (that is, shorten by width) = the anchor of lines? And how can I then search with grep by beginning of lines without fullscreening the terminal?

PS: I do not know why, when I declare the MANWIDTH var, it won't apply with man command immediately, despite it's value. That is, If i declare it first, and then try to man something (eventually pipe it to grep for lines search), The error arise again : troff: warning: can't break line

As this:

MANWIDTH=1000;
echo $MANWIDTH #output 1000
man gcc | grep -- '^-c'
troff: <standard input>:10635: warning [p 97, 19.0i]: can't break line

Solution

  • The -c is not at the beginning of the line because it is not at the left margin. You could use the pattern ^\s*-c which allows zero or more whitespace characters between the beginning of the line and the -c.

    But if you want to read the documentation for the -c command, you're better off using the info command, at least in this case:

    info gcc --index-search=c
    

    Note: For this to work, you need to have installed the gcc info pages. On Debian/Ubuntu systems, for example, you'll want to sudo apt-install gcc-doc.

    Note: the gcc info file authors chose not to put leading hyphens in index entries for most options. But most info files follow the convention of indexing with both with and with the dash (as gcc does with a few of the -f options). Although info will try to find the most relevant index match, info index searches are substring matches of literal strings (i.e. no regex operators) and sometimes you'll prefer to search with the dash:

    info grep --index-search=-E
    

    or to use the -all option to provide a listing of all matching index entries:

    info grep --all --index-search=E
    

    For utilities with fewer options than gcc, you might just go directly to the options listing with the -O option.

    info grep -O
    

    Finally, the answer to your PS:

    Use export MANWIDTH=1000 if you want MANWIDTH to be an environment variable visible to all subprocesses.