plgrep

A command-line utility for searching for regular expressions, which uses Perl regular expressions, which I made because the version of grep that comes with my computer (Mac OS X) doesn't support Perl-style regular expressions. It requires Perl (at least version 5.16.2 works). Its flags are not all compatible with other versions of grep. It's unrelated to any other programs with this name.

Download plgrep (10.85K) (source)

Flags

(This is for version 3; for earlier versions use --help.)

Basic syntax:
plgrep [-AcHhilLnruz -Iglob -Xglob --binary-files=(ignore|binary|text) --exclude-dir=glob --follow-links --max-chars=n --section=pattern --section-after=pattern] pattern [file ...]
plgrep [options] -e pattern [-e pattern ...] [-v pattern ...] [file ...]
plgrep [options] -v pattern [-v pattern ...] [file ...]

Output options

-H, --with-filename
include the filename in the output
-h, --no-filename
don't include the filename in the output
-n, --line-number
include the line numbers in the output
-c, --color, --colour
highlight the matching part of the line and the filename and line number at the beginning of each match. By default, the matched text is red, and the filename and line number have a red background (I chose red because it's readable against both light and dark backgrounds, at least to me), and certain messages are gray (which some terminals might not support); these can be changed by editing the source code (these are near the top).
-l, --files-with-matches
don't show matching lines, only show the names of the files with matching lines
-L, --files-without-match
don't show matching lines, only show the names of the files WITHOUT matching lines
--max-chars=n
display at most n characters per line around the match (minimum 7). This can be helpful if some files have really long "lines". In this version, the filename does not count towards the limit.
--section=pattern
displays the most recent line matching pattern along with each match. For instance, for code in many programming languages, --section='^\S' (i.e., previous unindented line) or --section='^[^\s{]' (i.e., previous unindented line that doesn't start with an open brace) will display which function the match is in; or --section='<h2>' will display the previous level-2 heading in an HTML file.
--section-after=pattern
displays the most recent line after a line matching pattern along with each match. Similar to --section, except the line displayed is the one after the one matched. For instance, if a document is divided into sections separated by blank lines, --section-after='^$' displays the first line of the section.

Interpretation of files

--binary-files=(ignore|binary|text)
determines how to deal with non-text files
  • ignore: pretend they don't exist
  • binary: display a message including the byte offset that matched (this is the default)
  • text: treat them as if they were text
-z, --compressed-text
look in compressed files, and treat them as containing text; supports .gz and .bz2 (file type determined by extension). This version can't tell the difference between compressed binary and compressed text; this option may change in future versions if I figure out how to do that. .tgz files are also treated as compressed text (which can work if the contents of the archive is text).
-u, --utf-8
treat both the patterns and files as UTF-8

Recursive mode

-r, -R, --recursive
search for all files within a directory. If any of the options in this section are specified, -r is implied. If -r is specified or implied and no explicit filename is given on the command line, defaults to searching the current directory rather than the standard input.
follow symbolic links found in directories. Links on the command line are always followed.
-A, --include-hidden
look in files and directories whose names start with a dot (.). Not specifying this option is equivalent to specifying --exclude=.* --exclude-dir=.*.
-I, --include=glob
only search in files matching the specified glob (* = any number of characters, ? = one character, and character classes can also be used)
-X, --exclude=glob
don't search in files matching the specified glob
--exclude-dir=glob
don't search in directories matching the specified glob

Pattern options

-e pattern
specifies the pattern. If this flag is used more than once, all patterns specified must match somewhere on each line for that line to match.
-v pattern, --invert-match=pattern
look for lines that do not contain the specified pattern. To match lines that contain one pattern, but do not contain some other pattern, use both -e and -v (non-matches always take precedence over matches). Note that this is not quite the same as normal grep -v: plgrep's -v takes the pattern as an argument, so plgrep -vi pattern searches for the lines not containing "i" in the file "pattern", rather than searching case-insensitively for lines not containing "pattern" in the standard input; for the latter interpretation, make -v the last option (plgrep -iv pattern).
-i, --ignore-case
treat all patterns as non-case-sensitive (A matches a). To ignore case in a specific pattern, put (?i) at the beginning.

Program information

--help
display an abbreviated version of this help on standard output (which you can pipe to other commands, e.g., plgrep --help | less). (Help displayed when you forget to type a pattern is displayed on standard error.)
--version
display the version. The latest version is 3.0 (2016-07-16).

Version history