Getting Started with ugrep: Installation, Syntax, and Examples
ugrep is a modern, feature-rich command-line text search tool compatible with GNU grep while adding powerful features like Perl-compatible regular expressions, file type filtering, Unicode support, and faster searches on many workloads. This guide walks through installation, essential syntax, and practical examples to get you productive quickly.
Why choose ugrep
- Speed: Optimized searching algorithms for many common cases.
- Rich regex support: PCRE2 and Perl-like features.
- File handling: Built-in file type detection, archive and compressed-file search.
- Contextual output: Colorized matches, line numbers, and smart previews.
Installation
macOS (Homebrew)
- Install Homebrew if needed.
- Run:
bash
brew install ugrep
Linux (Debian/Ubuntu)
- For recent distributions, use apt if ugrep is packaged:
bash
sudo apt updatesudo apt install ugrep
- If not available or you want the latest release, download a prebuilt binary or build from source:
bash
# example using a downloaded tarball (adjust version and URL)tar xvf ugrep--linux-x86_64.tar.gzsudo cp ugrep--linux-x86_64/ugrep /usr/local/bin/
Fedora / CentOS / RHEL
bash
sudo dnf install ugrep
If not packaged, build from source (see GitHub releases).
Windows
- Use the prebuilt Windows executable from the ugrep releases page and add it to your PATH, or install via Scoop/Chocolatey if available:
powershell
scoop install ugrep# orchoco install ugrep
Build from source (generic)
bash
git clone https://github.com/Genivia/ugrep.gitcd ugrepcmake -S . -B buildcmake –build build –config Releasesudo cmake –install build
Basic syntax
ugrep largely mirrors grep-style usage:
bash
ugrep [OPTIONS] PATTERN [FILES…]
Common options:
-Ror-r— recursive search in directories-n— show line numbers-H— show file names (on by default when multiple files)-i— case-insensitive-F— fixed-string (literal) search-P— use PCRE2 (Perl-compatible) regex-U— allow matching across NUL bytes (binary-safe)-M— multiline matching (with PCRE2)-C NUM— show NUM lines of context around matches–color=auto— colorize matches (on by default in many builds)
Examples
1) Simple search in a file
bash
ugrep “TODO” README.md
2) Recursive search in a project, with line numbers
bash
ugrep -R -n “fixme” .
3) Case-insensitive search
bash
ugrep -i “License”
4) Use PCRE2 features (lookarounds, named groups)
bash
ugrep -P -n “(?<=def\s)\w+(?=()” .py
This finds Python function names by using lookbehind and lookahead.
5) Fixed-string search for many patterns (fast)
bash
ugrep -F -f patterns.txt src/
Where patterns.txt contains one literal pattern per line.
6) Search inside compressed files and archives
ugrep can search inside many archive and compressed formats (zip, tar.gz, xz) without explicit decompression:
bash
ugrep -R “Copyright” /path/to/archives
7) Show context around matches
bash
ugrep -n -C 2 “error” /var/log/.log
8) Multiline matches (with PCRE2)
bash
ugrep -P -M “BEGIN\s+[\s\S]*?END” file.txt
9) Colorized preview with file and match highlights
bash
ugrep –color=always -n “initialize” src/ | less -R
10) Count matches per file
bash
ugrep -c “import” .py
Tips and best practices
- Use
-Ffor literal pattern files when searching many fixed strings — it’s faster and uses less CPU. - Prefer
-Pwhen you need advanced regex features; test expressions with small inputs first. - Combine
-rwith–excludeor–includeto narrow searches:
bash
ugrep -R –exclude-dir=.git –include=.py “TODO” .
- Use
-j Nto limit parallelism if you face I/O contention (if your ugrep build supports it).
Troubleshooting
- If behavior differs from GNU grep, check whether options like
-Por-Fare enabled — ugrep supports additional defaults. - If binary files appear, try
-a(treat binary files as text) or-Ufor NUL-safe processing. - For build issues, ensure dependencies for PCRE2 and CMake are installed.
Further reading
- Consult the ugrep manual or `ugrep –
Leave a Reply