Customizing the Output

Normally, the Unix platform, GNU tools, and Emacs editor family support the standard Unix-style grep output format. However, the DOS, Windows, and OS/2 platforms have widely varying (and often uneven) requirements for the output of grep and grep-like tools. Of course, GNU Emacs can be customized for different styles, but most other tools are not nearly as accomodating.

Therefore, the output of grep can be customized to include the filename, line number, column number, and other characters in the order required by different programs. If the -n flag is specified, then a format pattern number may also be specified. If no pattern number is specified, then -0 (zero) is used as the default.

The grep program looks in the GREPn environment variable, where n is the pattern number. If there is no GREPn variable in the environment (for instance, if -n5 is specified but there is no GREP5 environment variable defined), then the ``$n($l) : '' pattern is assumed.

The characters from the formatting pattern defined by the environment variable are copied to the output and then the text from the line is added. If a dollar sign $ meta character is encountered, then it and the next character are interpreted as follows:

$n specifies the name of the file.
$l (letter el) specifies the line number within the file.
$c specifies the starting column within the line.
$$ specifies the dollar sign itself.
$" specifies a double quote character.

With the exception of $", double quotes within or surrounding a format pattern are ignored.

If no format pattern is found, the ``$n($l) : '' pattern is assumed. This causes the following information to be shown:

 filename(linenumber) : text from line

Some useful format patterns that may be specified are:

 set GREP0="$n($l) : "
 set GREP1="$n($l:$c) : "
 set GREP2="$n:$l:"
 set GREP3="$n/$l/$c "
 set GREP4="$n,$l,$c "

When specified with the -n flag, the format patterns shown above are used as follows:

-n0 is the default that this package's grep has shown from the beginning. It mimics the format of error messages from many PC compilers:

 filename(linenumber) : text from line

The GREP0 environment variable can be changed to another pattern so that -n by itself uses that pattern instead.

-n1 mimics the format of error messages from some modern C/C++ compilers that also include the column number:

 filename(line:column) : text from line

-n2 is the Unix and GNU grep's style:

 filename:linenumber:text from line

-n3 and -n4 mimic the format used by the UltraEdit32 editor (for Windows platforms only) allows you to select a filename, open it, and put the cursor at the specified line and column within the file. UltraEdit is a very capable shareware editor available from the www.idmcomp.com Web site. It's a spectacular bargain with a low shareware price and with capabilities, ease-of-use, and company responsiveness far beyond that of many other editors at any price.