UniversalCodeGrep (ucg) is an extremely fast grep-like tool specialized for searching large bodies of source code.
UniversalCodeGrep (ucg) is an extremely fast grep-like tool specialized for searching large bodies of source code. It is intended to be largely command-line compatible with Ack, to some extent with ag, and where appropriate with grep. Search patterns are specified as PCRE regexes.
ucg is intended to address the impatient programmer's code searching needs. ucg is written in C++11 and takes advantage of the concurrency (and other) support of the language to increase scanning speed while reducing reliance on third-party libraries and increasing portability. Regex scanning is provided by the PCRE library, with its JIT compilation feature providing a huge performance gain on most platforms.
As a consequence of its use of these facilities and its overall design for maximum concurrency and speed, ucg is extremely fast. Under Fedora 23, scanning the Boost 1.58.0 source tree with ucg 0.2.2, ag 0.31.0, and ack 2.14 produces the following results:
| Command | Elapsed Real Time, Average of 5 Runs |
|---|---|
time ucg --noenv --cpp 'BOOST.*HPP' ~/src/boost_1_58_0 |
~ 0.404 seconds |
time ag --cpp 'BOOST.*HPP' ~/src/boost_1_58_0 |
~ 5.8862 seconds |
time ack --noenv --cpp 'BOOST.*HPP' ~/src/boost_1_58_0 |
~ 12.0398 seconds |
UniversalCodeGrep is in fact somewhat faster than grep itself. Again under Fedora 23 and searching the Boost 1.58.0 source tree, ucg bests grep 2.22 not only in ease-of-use but in raw speed:
| Command | Elapsed Real Time, Average of 5 Runs |
|---|---|
time grep -Ern --color --include=\*.cpp --include=\*.hpp --include=\*.h --include=\*.cc --include=\*.cxx 'BOOST.*HPP' ~/src/boost_1_58_0 |
~ 0.9852 seconds |
time ucg --noenv --cpp 'BOOST.*HPP' ~/src/boost_1_58_0 |
~ 0.404 seconds |
The resulting matches are identical.
If you are a Ubuntu user, the easiest way to install UniversalCodeGrep is from the Launchpad PPA here. To install from the command line:
# Add the PPA to your system:
sudo add-apt-repository ppa:grvs/ucg
# Pull down the latest lists of software from all archives:
sudo apt-get update
# Install ucg:
sudo apt-get install universalcodegrepIf you are a Red Hat, Fedora, or CentOS user, the easiest way to install UniversalCodeGrep is from the Fedora Copr-hosted dnf/yum repository here. Installation is as simple as:
# Add the Copr repo to your system:
sudo dnf copr enable grvs/UniversalCodeGrep
# Install UniversalCodeGrep:
sudo dnf install universalcodegrepBinary RPMs for openSUSE are available here.
UniversalCodeGrep can be built and installed from the distribution tarball (available here) in the standard autotools manner:
tar -xaf universalcodegrep-0.2.2.tar.gz
cd universalcodegrep-0.2.2.tar.gz
./configure
make
make installThis will install the ucg executable in /usr/local/bin. If you wish to install it elsewhere or don't have permissions on /usr/local/bin, specify an installation prefix on the ./configure command line:
./configure --prefix=~/<install-root-dir>Versions of gcc prior to 4.8 do not have sufficiently complete C++11 support to build ucg.
This should be available from your Linux distro.
UniversalCodeGrep should build and function anywhere the prerequisites are available. It has been built and tested on the following OSes/distros:
- Linux
- Ubuntu 15.04
- CentOS 7
- Fedora 22
- Fedora 23
- RHEL 7
- SLE 12
- openSUSE 13.2
- openSUSE Leap 42.1
- Windows 7 + Cygwin 64-bit (Note however that speed here is comparable to
ag)
Invoking ucg is the same as with ack or ag:
ucg [OPTION...] PATTERN [FILES OR DIRECTORIES]...where PATTERN is an PCRE-compatible regular expression.
If no FILES OR DIRECTORIES are specified, searching starts in the current directory.
Version 0.2.2 of ucg supports a significant subset of the options supported by ack. Future releases will have support for more options.
| Option | Description |
|---|---|
--[no]smart-case |
Ignore case if PATTERN is all lowercase (default: enabled). |
-i, --ignore-case |
Ignore case distinctions in PATTERN. |
-Q, --literal |
Treat all characters in PATTERN as literal. |
-w, --word-regexp |
PATTERN must match a complete word. |
| Option | Description |
|---|---|
--column |
Print column of first match after line number. |
--nocolumn |
Don't print column of first match (default). |
| Option | Description |
|---|---|
--color, --colour |
Render the output with ANSI color codes. |
--nocolor, --nocolour |
Render the output without ANSI color codes. |
| Option | Description |
|---|---|
--ignore-dir=name, --ignore-directory=name |
Exclude directories with this name. |
--noignore-dir=name, --noignore-directory=name |
Do not exclude directories with this name. |
-k, --known-types |
Only search in files of recognized types (default: on). |
-n, --no-recurse |
Do not recurse into subdirectories. |
-r, -R, --recurse |
Recurse into subdirectories (default: on). |
--type=[no]TYPE |
Include only [exclude all] TYPE files. Types may also be specified as --[no]TYPE. |
| Option | Description |
|---|---|
--type-add=TYPE:FILTER:FILTERARGS |
Files FILTERed with the given FILTERARGS are treated as belonging to type TYPE. Any existing definition of type TYPE is appended to. |
--type-del=TYPE |
Remove any existing definition of type TYPE. |
--type-set=TYPE:FILTER:FILTERARGS |
Files FILTERed with the given FILTERARGS are treated as belonging to type TYPE. Any existing definition of type TYPE is replaced. |
| Option | Description |
|---|---|
-j, --jobs=NUM_JOBS |
Number of scanner jobs (std::thread<>s) to use. |
--noenv |
Ignore .ucgrc files. |
| Option | Description |
|---|---|
-?, --help |
give this help list |
--help-types, --list-file-types |
Print list of supported file types. |
--usage |
give a short usage message |
-V, --version |
print program version |
UniversalCodeGrep supports configuration files with the name .ucgrc, in which command-line options can be stored on a per-user and per-directory-hierarchy basis.
.ucgrc files are text files with a simple format. Each line of text can be either:
- A single-line comment. The line must start with a
#and the comment continues for the rest of the line. - A command-line parameter. This must be exactly as if it was given on the command line.
When ucg is invoked, it looks for command-line options from the following locations in the following order:
- The
.ucgrcfile in the user's$HOMEdirectory, if any. - The first
.ucgrcfile found, if any, by walking up the component directories of the current working directory. This traversal stops at either the user's$HOMEdirectory or the root directory. This is called the project config file, and is intended to live in the top-level directory of a project directory hierarchy. - The command line itself.
Options read later will override earlier options.
ucg supports user-defined file types with the --type-set=TYPE:FILTER:FILTERARGS and --type-add=TYPE:FILTER:FILTERARGS command-line options. Only two FILTERs are currently supported, ext (extension list) and is (literal filename).
The extension list filter allows you to specify a comma-separated list of file extensions which are to be considered as belonging to file type TYPE.
Example:
--type-set=type1:ext:abc,xqz,def
The literal filename filter simply specifies a single literal filename which is to be considered as belonging to file type TYPE.
Example:
--type-add=autoconf:is:configure.ac