ugrep

ugrep

高效全能的开源文件搜索工具

ugrep是一款开源的高性能文件搜索工具,支持Unicode和多行匹配。它能搜索压缩文件和PDF等文档,提供交互式TUI界面和类Google的布尔查询。ugrep完全兼容GNU grep,并增加了模糊搜索、二进制文件搜索等新功能。其高效的DFA正则表达式引擎保证了快速的搜索速度。ugrep支持JSON、XML等多种输出格式,是一个功能全面的现代grep替代工具。

ugrep文本搜索正则表达式命令行工具开源软件Github开源项目
<p align="center"> <a href="https://github.com/Genivia/ugrep/actions/workflows/c-cpp.yml"><img src="https://github.com/Genivia/ugrep/actions/workflows/c-cpp.yml/badge.svg"></a> <a href="https://opensource.org/licenses/BSD-3-Clause"><img src="https://img.shields.io/badge/license-BSD%203--Clause-blue.svg"></a> </p> <h1 align="center">The ugrep file pattern searcher</h1> <p align="center"> [ <b>README</b> | <a href="https://ugrep.com">User&nbsp;Guide</a> | <a href="https://github.com/Genivia/ugrep-indexer">Indexing</a> | <a href="https://github.com/Genivia/ugrep-benchmarks">Benchmarks</a> | <a href="https://github.com/Genivia/ugrep/discussions/categories/q-a">Q&amp;A</a> ] <br> <br> <img src="https://www.genivia.com/images/scranim.gif" width="438" alt=""> <br> <i>option -Q opens a query TUI to search files as you type!</i> </p>

Why use ugrep?

  • ugrep is fast, user-friendly, and equipped with a ton of new features that users wanted

  • includes an interactive TUI with built-in help, Google-like search with AND/OR/NOT patterns, fuzzy search, searches (nested) zip/7z/tar/pax/cpio archives, tarballs and compressed files gz/Z/bz/bz2/lzma/xz/lz4/zstd/brotli, search and hexdump binary files, search documents such as PDF, doc, docx, and output in JSON, XML, CSV or your own customized format

  • Unicode extended regex pattern syntax with multi-line pattern matching without requiring special command-line options

  • includes a file indexer to speed up searching slow and cold file systems

  • a true drop-in replacement for GNU grep (assuming you copy or symlink ug to grep, and to egrep and to fgrep), unlike other popular grep claiming to be "grep alternatives" or "replacements" when those actually implement incompatible command-line options and use an incompatible regex matcher, i.e. Perl regex only versus POSIX BRE (grep) and ERE (egrep) when ugrep supports all regex modes

  • benchmarks show that ugrep is (one of) the fastest grep using the high-performance DFA-based regex matcher RE/flex

Development roadmap

if something should be improved or added to ugrep, then let me know!

Overview

Commands

  • ug is for interactive use, which loads an optional .ugrep configuration file with your preferences located in the working directory or home directory, ug+ also searches pdfs, documents, e-books, image metadata

  • ugrep for batch use like GNU grep without a .ugrep configuration file, ugrep+ also searches pdfs, documents, e-books, image metadata

What does ugrep add that GNU grep does not support?

  • Matches Unicode patterns by default and automatically searches UTF-8, UTF-16 and UTF-32 encoded files

  • Matches multiple lines with \n or \R in regex patterns, no special options are required to do so!

  • Built-in help: ug --help, where ug --help WHAT displays options related to WHAT you are looking for

    💡 ug --help regex, ug --help globs, ug --help fuzzy, ug --help format.

  • User-friendly with customizable configuration files used by the ug command intended for interactive use that loads a .ugrep configuration file with your preferences

    ug PATTERN ...                         ugrep --config PATTERN ...
    

    💡 ug --save-config ...options-you-want-to-save... saves a .ugrep config file in the working directory so that the next time you run ug there it uses these options. Do this in your home directory to save a .ugrep config file with options you generally want to use.

  • Interactive query TUI, press F1 or CTRL-Z for help and TAB/SHIFT-TAB to navigate to dirs and files

    ug -Q                                  ug -Q -e PATTERN
    

    💡 -Q replaces PATTERN on the command line to let you enter patterns interactively in the TUI. In the TUI use ALT+letter keys to toggle short "letter options" on/off, for example ALT-n (option -n) to show/hide line numbers.

  • Search the contents of archives (zip, tar, pax, jar, cpio, 7z) and compressed files (gz, Z, bz, bz2, lzma, xz, lz4, zstd, brotli)

    ug -z PATTERN ...                      ug -z --zmax=2 PATTERN ...
    

    💡 specify -z --zmax=2 to search compressed files and archives nested within archives. The --zmax argument may range from 1 (default) to 99 for up to 99 decompression and de-archiving steps to search nested archives

  • Search with Google-like Boolean query patterns using -% patterns with AND (or just space), OR (or a bar |), NOT (or a dash -), using quotes to match exactly, and grouping with ( ) (shown on the left side below); or with options -e (as an "or"), --and, --andnot, and --not regex patterns (shown on the right side below):

    ug -% 'A B C' ...                      ug -e 'A' --and 'B' --and 'C' ...
    ug -% 'A|B C' ...                      ug -e 'A' -e 'B' --and 'C' ...
    ug -% 'A -B -C' ...                    ug -e 'A' --andnot 'B' --andnot 'C' ...
    ug -% 'A -(B|C)'...                    ug -e 'A' --andnot 'B' --andnot 'C' ...
    ug -% '"abc" "def"' ...                ug -e '\Qabc\E' --and '\Qdef\E' ...
    

    where A, B and C are arbitrary regex patterns (use option -F to search strings)

    💡 specify option -%% (--bool --files) to apply the Boolean query to files as a whole: a file matches if all Boolean conditions are satisfied by matching patterns file-wide. Otherwise, Boolean conditions apply to single lines by default, since grep utilities are generally line-based pattern matchers. Option --stats displays the query in human-readable form after the search completes.

  • Search pdf, doc, docx, e-book, and more with ug+ using filters associated with filename extensions:

    ug+ PATTERN ...
    

    or specify --filter with a file type to use a filter utility:

    ug --filter='pdf:pdftotext % -' PATTERN ...
    ug --filter='doc:antiword %' PATTERN ...
    ug --filter='odt,docx,epub,rtf:pandoc --wrap=preserve -t plain % -o -' PATTERN ...
    ug --filter='odt,doc,docx,rtf,xls,xlsx,ppt,pptx:soffice --headless --cat %' PATTERN ...
    ug --filter='pem:openssl x509 -text,cer,crt,der:openssl x509 -text -inform der' PATTERN ...
    ug --filter='latin1:iconv -f LATIN1 -t UTF-8' PATTERN ...
    

    💡 the ug+ command is the same as the ug command, but also uses filters to search PDFs, documents, and image metadata

  • Display horizontal context with option -o (--only-matching) and context options -ABC, e.g. to find matches in very long lines, such as Javascript and JSON sources:

    ug -o -C20 -nk PATTERN longlines.js
    

    💡 -o -C20 fits all matches with context in 20 characters before and 20 charactess after a match (i.e. 40 Unicode characters total), -nk outputs line and column numbers.

  • Find approximate pattern matches with fuzzy search, within the specified Levenshtein distance

    ug -Z PATTERN ...                      ug -Z3 PATTTERN ...
    

    💡 -Zn matches up to n extra, missing or replaced characters, -Z+n matches up to n extra characters, -Z-n matches with up to n missing characters and -Z~n matches up to n replaced characters. -Z defaults to -Z1.

  • Fzf-like search with regex (or fixed strings with -F), fuzzy matching with up to 4 extra characters with -Z+4 and words only with -w, using -%% for file-wide Boolean searches

    ug -Q -%% -l -w -Z+4 --sort=best
    

    💡 -l lists the matching files in the TUI, press TAB then ALT-y to view a file, SHIFT-TAB and Alt-l to go back to view the list of matching files ordered by best match

  • Search binary files and display hexdumps with binary pattern matches (Unicode text or -U for byte patterns)

    ug --hexdump -U BYTEPATTERN ...        ug --hexdump TEXTPATTERN ...
    ug -X -U BYTEPATTERN ...               ug -X TEXTPATTERN ...
    ug -W -U BYTEPATTERN ...               ug -W TEXTPATTERN ...
    

    💡 --hexdump=4chC1 displays 4 columns of hex without a character column c, no hex spacing h, and with one extra hex line C1 before and after a match.

  • Include files to search by file types or file "magic bytes" or exclude them with ^

    ug -t TYPE PATTERN ...                 ug -t ^TYPE PATTERN ...
    ug -M 'MAGIC' PATTERN ...              ug -M '^MAGIC' PATTERN ...
    
  • Include files and directories to search that match gitignore-style globs or exclude them with ^

    ug -g 'FILEGLOB' PATTERN ...           ug -g '^FILEGLOB' PATTERN ...
    ug -g 'DIRGLOB/' PATTERN ...           ug -g '^DIRGLOB/' PATTERN ...
    ug -g 'PATH/FILEGLOB' PATTERN ...      ug -g '^PATH/FILEGLOB' PATTERN ...
    ug -g 'PATH/DIRGLOB/' PATTERN ...      ug -g '^PATH/DIRGLOB/' PATTERN ...
    
  • Include files to search by filename extensions (suffix) or exclude them with ^, a shorthand for -g"*.EXT"

    ug -O EXT PATTERN ...                  ug -O ^EXT PATTERN ...
    
  • Include hidden files (dotfiles) and directories to search (omitted by default)

    ug -. PATTERN ...                      ug -g'.*,.*/' PATTERN ...
    

    💡 specify hidden in your .ugrep to always search hidden files with ug.

  • Exclude files specified by .gitignore etc.

    ug --ignore-files PATTERN ...          ug --ignore-files=.ignore PATTERN ...
    

    💡 specify ignore-files in your .ugrep to always ignore them with ug. Add additional ignore-files=... as desired.

  • Search patterns excluding negative patterns ("match this but not that")

    ug -e PATTERN -N NOTPATTERN ...        ug -e '[0-9]+' -N 123 ...
    
  • Use predefined regex patterns to search source code, javascript, XML, JSON, HTML, PHP, markdown, etc.

    ug PATTERN -f c++/zap_comments -f c++/zap_strings ...
    ug PATTERN -f php/zap_html ...
    ug -f js/functions ... | ug PATTERN ...
    
  • Sort matching files by name, best match, size, and time

    ug --sort PATTERN ...                  ug --sort=size PATTERN ...
    ug --sort=changed PATTERN ...          ug --sort=created PATTERN ...
    ug -Z --sort=best PATTERN ...          ug --no-sort PATTERN ...
    
  • Output results in CSV, JSON, XML, and user-specified formats

    ug --csv PATTERN ...                   ug --json PATTERN ...
    ug --xml PATTERN ...                   ug --format='file=%f line=%n match=%O%~' PATTERN ...
    

    💡 ug --help format displays help on format % fields for customized output.

  • Search with PCRE's Perl-compatible regex patterns and display or replace subpattern matches

    ug -P PATTERN ...                      ug -P --format='%1 and %2%~' 'PATTERN(SUB1)(SUB2)' ...
    
  • Replace patterns in the output with -P and --replace replacement text, optionally containing % formatting fields, using -y to pass the rest of the file through:

    ug --replace='TEXT' PATTERN ...        ug -y --replace='TEXT' PATTERN ...
    ug --replace='(%m:%o)' PATTERN ...     ug -y --replace='(%m:%o)' PATTERN ...
    ug -P --replace='%1' PATTERN ...       ug -y -P --replace='%1' PATTERN ...
    

    💡 ug --help format displays help on format % fields to optionally use with --replace.

  • Search files with a specific encoding format such as ISO-8859-1 thru 16, CP 437, CP 850, MACROMAN, KOI8, etc.

    ug --encoding=LATIN1 PATTERN ...
    
<a name="toc"/>

Table of contents

编辑推荐精选

TRAE编程

TRAE编程

AI辅助编程,代码自动修复

Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。

AI工具TraeAI IDE协作生产力转型热门
商汤小浣熊

商汤小浣熊

最强AI数据分析助手

小浣熊家族Raccoon,您的AI智能助手,致力于通过先进的人工智能技术,为用户提供高效、便捷的智能服务。无论是日常咨询还是专业问题解答,小浣熊都能以快速、准确的响应满足您的需求,让您的生活更加智能便捷。

imini AI

imini AI

像人一样思考的AI智能体

imini 是一款超级AI智能体,能根据人类指令,自主思考、自主完成、并且交付结果的AI智能体。

Keevx

Keevx

AI数字人视频创作平台

Keevx 一款开箱即用的AI数字人视频创作平台,广泛适用于电商广告、企业培训与社媒宣传,让全球企业与个人创作者无需拍摄剪辑,就能快速生成多语言、高质量的专业视频。

即梦AI

即梦AI

一站式AI创作平台

提供 AI 驱动的图片、视频生成及数字人等功能,助力创意创作

扣子-AI办公

扣子-AI办公

AI办公助手,复杂任务高效处理

AI办公助手,复杂任务高效处理。办公效率低?扣子空间AI助手支持播客生成、PPT制作、网页开发及报告写作,覆盖科研、商业、舆情等领域的专家Agent 7x24小时响应,生活工作无缝切换,提升50%效率!

蛙蛙写作

蛙蛙写作

AI小说写作助手,一站式润色、改写、扩写

蛙蛙写作—国内先进的AI写作平台,涵盖小说、学术、社交媒体等多场景。提供续写、改写、润色等功能,助力创作者高效优化写作流程。界面简洁,功能全面,适合各类写作者提升内容品质和工作效率。

AI辅助写作AI工具蛙蛙写作AI写作工具学术助手办公助手营销助手AI助手
问小白

问小白

全能AI智能助手,随时解答生活与工作的多样问题

问小白,由元石科技研发的AI智能助手,快速准确地解答各种生活和工作问题,包括但不限于搜索、规划和社交互动,帮助用户在日常生活中提高效率,轻松管理个人事务。

热门AI助手AI对话AI工具聊天机器人
Transly

Transly

实时语音翻译/同声传译工具

Transly是一个多场景的AI大语言模型驱动的同声传译、专业翻译助手,它拥有超精准的音频识别翻译能力,几乎零延迟的使用体验和支持多国语言可以让你带它走遍全球,无论你是留学生、商务人士、韩剧美剧爱好者,还是出国游玩、多国会议、跨国追星等等,都可以满足你所有需要同传的场景需求,线上线下通用,扫除语言障碍,让全世界的语言交流不再有国界。

讯飞智文

讯飞智文

一键生成PPT和Word,让学习生活更轻松

讯飞智文是一个利用 AI 技术的项目,能够帮助用户生成 PPT 以及各类文档。无论是商业领域的市场分析报告、年度目标制定,还是学生群体的职业生涯规划、实习避坑指南,亦或是活动策划、旅游攻略等内容,它都能提供支持,帮助用户精准表达,轻松呈现各种信息。

AI办公办公工具AI工具讯飞智文AI在线生成PPTAI撰写助手多语种文档生成AI自动配图热门
下拉加载更多