A Grip on Grep

Swiss Army Knife considered harmful

grep

Filters

Overview

Filters read input line by line and match each line against a set of pattern.

Filters serve two purposes:

  1. They detect if a line matches the pattern set
  2. They provide information about matched lines on output

Filters exit with code 0 (success) if any line matched any pattern. If no line matched the exit code is 1 (failure), on error a code >1 is returned. The caller can request to invert the match logic: finding a match is a failure, not finding one as success.

Types of information provided by filters:

Note: already implemented features are marked in bold.

Filter micro languages

    Match Information Formats

    By default, filters output all matching lines unaltered.

    Another simple case is the number of matching lines: if requested, the filter only outputs this information as decimal number.

    Any other information is output in the following way:

    For matched/unmatched lines, the prefix is followed immediately by the 1 based line number in decimal notation, followed by a colon (':'), followed by the zero based byte offset of the line in decimal notation, followed by a colon.

    If match string information is requested, each matched line is followed by the list of matched strings in order of detection, each match on a successive line with a '#' prefix.

    The format of the match string report is:

    #line:id:string:index:len:offset:num:type:pattern

    Where:

    If string or pattern contain a colon or a backslash, they are escaped with a backslash.

    id can be the sequence number of the match in decimal notation.

    All fields after string are optional.

    Command Line Interface

    filter [output_type] [behavior] [output_options] [filter_options] [pattern ..]

    filter [-c|-q] [-v][-m n][-0] [-n][-N][-p prefix][-z]  [-i][-w][-x] [pattern ..]

    output type:

    behavior:

    output options:

    filter options:

    A filter does not need to implement any option. The following option names are reserved for the respective functionality

    All patterns are specified as arguments on the command line.  If no pattern is given, nothing is selected. The filter implementation must specify clearly the meaning of an empty pattern. Some filters don't match any string, others match everything in this case.