glark - Search text files for complex regular expressions glark [options] expression file ... Similar to "grep", "glark" offers: Perl-compatible regular expressions, color highlighting of matches, context around matches, complex expressions ("and" and "or"), grep output emulation, and automatic exclusion of non-text files. Its regular expressions should be familiar to persons experienced in Perl, Python, or Ruby. Display the help message. Print NUM lines after a matched expression. Restrict the search to before the given location, which represents either the number of the last line within the valid range, or the percentage of lines to be searched. Specify how to handle binary files, thus overriding the default behavior, which is to denote the binary files that match the expression, without displaying the match. TYPE may be one of: "binary", the default; "without-match", which results in binary files being skipped; and "text", which results in the binary file being treated as text, the display of which may have bad side effects with the terminal. Note that the default behavior has changed; this previously was to skip binary files. The same effect may be achieved by setting binary-files to "without-match" in the ~/.glarkrc file. Print NUM lines before a matched expression. Output NUM lines of context around a matched expression. The default is no context. If no NUM is given for this option, the number of lines of context is 2. Instead of normal output, display only the number of matches in each file. Directories are processed according to the given ACTION, which by default is "read". If ACTION is "recurse", each file in the directory is read and each subdirectory is recursed into (equivalent to the -r option). If ACTION is "skip", directories are not read, and no message is produced. Restrict the search to after the given section, which represents either the number of the first line within the valid range, or the percentage of lines to be skipped. Specify the highlight color for file names. See the HIGHLIGHTING section for more information. Produce output like the grep default: file names, no line numbers, and a single line of the match, which will be the first line for matches that span multiple lines. If the EMACS environment variable is set, this value is set to true. Thus, running glark under Emacs results in the output format expected by Emacs. Do not display the names of the files that matched. Display the names of the files that matched. This is the default behavior. Match regular expressions without regard to case. The default is case sensitive. Print only the names of the file that matched the expression. Print only the names of the file that did not match the expression. Find only the first NUM matches in each file. Do not search files whose names match the given expression. This can be useful for finding external references to a file, or to a class (assuming that class names match file names). Display the line numbers. This is the default behavior. Do not display the line numbers. Suppress warnings. Enable warnings. This is the default. Recurse through directories. Equivalent to --directories=read. Restrict the search to the given range of lines. Specify the highlight color for text. See the HIGHLIGHTING section for more information. Turn highlighting on, which uses ANSI escape sequences. This is the default behavior. See the HIGHLIGHTING section for more information. Turn highlighting off. Show lines that do not match the expression. Display version information. Put word boundaries around each pattern, thus matching only where the full word(s) occur in the text. Thus, "glark -w Foo" is the same as "glark '/\bFoo\b/'". Do not put word boundaries around each pattern. This is the default. Select only where the entire line matches the pattern(s). Do not select only where the entire line matches the pattern(s). This is the default behavior. Display only the region that matched, not the entire line. If the expression contains "backreferences" (i.e., expressions bounded by "( ... )", then only the portion captured will be displayed, not the entire line. This option is useful with "-g", which eliminates the default highlighting and display of file names. When in -l mode, write file names followed by the ASCII NUL character ('\0') instead of '\n'. Display normally suppressed output, for debugging purposes.

Regular Expressions
Regular expressions are expected to be in the Perl/Ruby format. Run "perldoc perlre" for more general information. The expression may be of either form: something /something/ There is no difference between the two forms, except that with the latter, one can provide the "ignore case" modifier, thus matching "someThing" and "SoMeThInG": % glark /something/i Note that this is redundant with the "-i" ("--ignore-case") option. All regular expression characters and options are available, such as "\w", ".*?" and "[^9]". For example: % glark '\b[a-z][^\d]\d{1,3}.*\s*>>\s*\d+\s*.*& +\d{3}' If the and and or options are not used, the last non-option is considered to be the expression to be matched. In the following, "printf" is used as the expression. % glark -w printf *.c POSIX character classes (e.g., [[:alpha:]]) are also supported.

Complex Expressions
Complex expressions combine regular expressions (and complex expressions themselves) with logical AND and OR operators. Matches either of the two expressions. The results of the two forms are equivalent. In the latter syntax, the --end-of-or is optional. Matches both of the two expressions, within <number> lines of each other. As with the or option, the results of the two forms are equivalent, and the --end-of-and is optional. If the number provided is -1 (negative one), the distance is considered to be "infinite", and thus, the condition is satisfied if both expressions match within the same file. If the number provided is 0 (zero), the condition is satisfied if both expressions match on the same line. A warning will be issued if the value given in the number position does not appear to be numeric. This option writes the expression in a more legible format. It can useful for debugging. Matching patterns and file names can be highlighted using ANSI escape sequences. Both the foreground and the background colors may be specified, from the following: black blue cyan green magenta red white yellow The foreground may have any number of the following modifiers applied: blink bold concealed reverse underline underscore The format is "MODIFIERS FOREGROUND on BACKGROUND". For example: red black on yellow (the default for patterns) reverse bold (the default for file names) green on white bold underline red on cyan By default text is highlighted as black on yellow. File names are written in reversed bold text.

Basic Usage
Searches for "format" in the local .h files. Searches for "format" without regard to case. Short form: % glark -i format *.h Produces 6 lines of context around any match for "format". Short forms: % glark -C 6 format *.h % glark -6 format *.h Find references to "Object", excluding the files whose names match "Object". Thus, SessionBean.java would be searched; EJBObject.java would not. Short form: % glark -M Object *.java Show where exceptions are dumped. Note that the "--grep" option is used, thus turning off highlighting and display of file names. If the "--no-filename" option is used, the output will consist of only the matching portions. The short form of this command is: % glark -gy '\w+\.printStackTrace\(.*\)' *.java Show only the variable name of exceptions that are dumped. Short form: % glark -gy '(\w+)\.printStackTrace\(.*\)' *.java Display only the names of users who logged in today. Displays (only) the names of the text files that contain "words" at least 25 characters long. Displays (only) the names of the files that do not contain strings consisting of a single word. Short form: % glark -L '"\w+"'

Highlighting
Display (in red text on a white background) occurrences of exactly 5 digits. Short form: % glark -T "red on white" '\b\d{5}\b' *.c See the HIGHLIGHTING section for valid colors and modifiers.

Complex Expressions
Searches for either "printf" or "format". Short form: % glark -o format print *.h Searches for both "printf" or "format" within 4 lines of each other. Short form: % glark -a 4 printf format *.c *.h Searches for both "printf" or "format" on the same line ("within 0 lines of each other"). Three lines of context are displayed around any matches. Short form: % glark -3 -a 0 printf format *.c (In order of the options:) Produces 8 lines of context around case insensitive matches of ("phtx" within 2 lines of '...' (literal)) within 15 lines of (either "va_\w+t" or "die"). Looks for "#define\s+YIELD" within the same file (-1 == "infinite distance") of "#define\s+dTHR". Short form: % glark -a -1 '#define\s+YIELD' '#define\s+dTHR' *.h

Range Limiting
Find references to "cout", within the first half of the file. Short form: % glark -b 50% cout *.cpp Find references to "cout", starting at the 20th line in the file. Short form: % glark -b 50% cout *.cpp Find references to "cout", in the first half of the file, after the 20th line. Short form: % glark -R 20 50% cout *.cpp A string of whitespace-delimited options. Due to parsing constraints, should probably not contain complex regular expressions. A resource file, containing name/value pairs, separated by either ':' or '='. The valid fields of a .glarkrc file are as follows, with example values: after-context: 1 before-context: 6 context: 5 file-color: blue on yellow highlight: off ignore-case: false quiet: yes text-color: bold reverse verbose: false grep: true "yes" and "on" are synonymnous with "true". "no" and "off" signify "false". My ~/.glarkrc file is the following: file-color: bold reverse text-color: bold black on yellow context: 2 highlight: on verbose: false ignore-case: false quiet: yes word: false binary-files: without-match

Exclusion of Non-Text Files
Non-text files are automatically skipped, by taking a sample of the file and checking for an excessive number of non-ASCII characters. For speed purposes, this test is skipped for files whose suffixes are associated with text files: txt, c, cpp, mk, h, hpp, html, and java. Similarly, this test is also skipped for files whose suffixes are associated with non-text (binary) files: a, o, obj, class, elc, gif, gz, jar, jpg, jpeg, png, pdf, tar, and Z. In the code, see @@KNOWN_TEXT and @@KNOWN_NONTEXT for the extensions of files that are presumed to be text and nontext. This list may be easily modified. The exit status is 0 if matches were found; 1 if no matches were found, and 2 if there was an error. An inverted match (the -v/--invert-match option) will result in 1 for matches found, 0 for none found. For regular expressions, the "perlre" man page. Mastering Regular Expressions, by Jeffrey Friedl, published by O'Reilly. "Unbalanced" leading and trailing slashes will result in those slashes being included as characters in the regular expression. Thus, the following pairs are equivalent: /foo "/foo" /foo\/ "/foo/" /foo\/i "/foo/i" foo/ "foo/" foo/ "foo/" The code to detect nontext files assumes ASCII, not Unicode. Jeff Pace <jpace@glark.org> Copyright (c) 2002, Jeff Pace. All Rights Reserved. This module is free software. It may be used, redistributed and/or modified under the terms of the Lesser GNU Public License. See http://www.gnu.org/licenses/lgpl.html for more information.