- Notifications
You must be signed in to change notification settings - Fork116
🔍 ugrep 7.3 file pattern searcher -- a more powerful, ultra fast, user-friendly, compatible grep replacement. Includes a TUI, Google-like Boolean search with AND/OR/NOT, fuzzy search, hexdumps, searches (nested) archives (zip, 7z, tar, pax, cpio), compressed files (gz, Z, bz2, lzma, xz, lz4, zstd, brotli), pdfs, docs, and more
License
Genivia/ugrep
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
[README |User Guide |Indexing |Benchmarks |Q&A ]
option -Q opens a query TUI to search files as you type!
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, search (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 youcopy or symlink
ug
togrep
, and toegrep
and tofgrep
), 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 modesbenchmarks show thatugrep is (one of) the fastest grep using the high-performance DFA-based regex matcherRE/flex
if something should be improved or added to ugrep, then let me know!
#1 priority is quality assurance to continue to make sure ugrep has no bugs and is reliable
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 metadataugrep
for batch use like GNU grep without a .ugrep configuration file,ugrep+
also searches pdfs, documents, e-books, image metadata
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
, whereug --help WHAT
displays options related toWHAT
you are looking for💡
ug --help regex
,ug --help globs
,ug --help fuzzy
,ug --help format
.User-friendly with customizableconfiguration files used by the
ug
command intended for interactive use that loads a .ugrep configuration file with your preferencesug 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 runug
there it uses these options. Do this in your home directory to save a .ugrep config file with options you generally want to use.Interactivequery 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
replacesPATTERN
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 ofarchives (zip, tar, pax, jar, cpio, 7z) andcompressed 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 archivesSearch with Google-likeBoolean query patterns using
-%
patterns withAND
(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
andC
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 theug
command, but also uses filters to search PDFs, documents, and image metadataDisplay 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 withfuzzy search, within the specified Levenshtein distance
ug -Z PATTERN ... ug -Z3 PATTTERN ...
💡
-Zn
matches up ton
extra, missing or replaced characters,-Z+n
matches up ton
extra characters,-Z-n
matches with up ton
missing characters and-Z~n
matches up ton
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 searchesug -Q -%% -l -w -Z+4 --sort=best
💡
-l
lists the matching files in the TUI, pressTAB
thenALT-y
to view a file,SHIFT-TAB
andAlt-l
to go back to view the list of matching files ordered by best matchSearchbinary 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
displays4
columns of hex without a character columnc
, no hex spacingh
, and with one extra hex lineC1
before and after a match.Include files to search byfile 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 matchgitignore-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 byfilename extensions (suffix) or exclude them with
^
, a shorthand for-g"*.EXT"
ug -O EXT PATTERN ... ug -O ^EXT PATTERN ...
Includehidden files (dotfiles) and directories to search (omitted by default)
ug -. PATTERN ... ug -g'.*,.*/' PATTERN ...
💡 specify
hidden
in your .ugrep to always search hidden files withug
.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 withug
. Add additionalignore-files=...
as desired.Search patterns excludingnegative patterns ("match this but not that")
ug -e PATTERN -N NOTPATTERN ... ug -e '[0-9]+' -N 123 ...
Usepredefined 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 byname, 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 inCSV, JSON, XML, anduser-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 replacesubpattern 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 specificencoding format such as ISO-8859-1 thru 16, CP 437, CP 850, MACROMAN, KOI8, etc.
ug --encoding=LATIN1 PATTERN ...
- How to install
- Performance comparisons
- Using ugrep within Vim
- Using ugrep within Emacs
- Using ugrep to replace GNU/BSD grep
- Tutorial
- Examples
- Advanced examples
- Displaying helpful info
- Configuration files
- Interactive search with -Q
- Recursively list matching files with -l, -R, -r, --depth, -g, -O, and -t
- Boolean query patterns with -%, -%%, --and, --not
- Search this but not that with -v, -e, -N, -f, -L, -w, -x
- Search non-Unicode files with --encoding
- Matching multiple lines of text
- Displaying match context with -A, -B, -C, and -y
- Searching source code using -f, -O, and -t
- Searching compressed files and archives with -z
- Find files by file signature and shebang "magic bytes" with -M, -O and -t
- Fuzzy search with -Z
- Search hidden files with -.
- Using filter utilities to search documents with --filter
- Searching and displaying binary files with -U, -W, and -X
- Ignore binary files with -I
- Ignoring .gitignore-specified files with --ignore-files
- Using gitignore-style globs to select directories and files to search
- Including or excluding mounted file systems from searches
- Counting the number of matches with -c and -co
- Displaying file, line, column, and byte offset info with -H, -n, -k, -b, and -T
- Displaying colors with --color and paging the output with --pager
- Output matches in JSON, XML, CSV, C++
- Customize output with --format
- Replacing matches with -P --replace and --format using backreferences
- Limiting the number of matches with -1,-2...-9, -K, -m, and --max-files
- Matching empty patterns with -Y
- Case-insensitive matching with -i and -j
- Sort files by name, best match, size, and time
- Tips for advanced users
- More examples
- Man page
- Regex patterns
- Troubleshooting
Install the latest ugrep withHomebrew:
$ brew install ugrep
or install withMacPorts:
$ sudo port install ugrep
This installs theugrep
andug
commands, whereug
is the same asugrep
but also loads the configuration file .ugrep when present in the workingdirectory or home directory.
Install withWingetwinget install Genivia.ugrep
Or install withChocolateychoco install ugrep
Or install withScoopscoop install ugrep
Or download the full-featuredugrep.exe
executable as release artifact fromhttps://github.com/Genivia/ugrep/releases. The zipped release contains themainugrep.exe
binary as well asug.exe
. Theug
command, intended forinteractive use, loads and reads in settings from the.ugrep
configurationfile (when present in the working directory or home directory).
Addugrep.exe
andug.exe
to your execution path: go toSettings andsearch for "Path" inFind a Setting. Selectenvironment variables ->Path ->New and add the directory where you placed theugrep.exe
andug.exe
executables.
Tip
Practical hints on usingugrep.exe
andug.exe
on the Windows command line:
- when quoting patterns and arguments on the command line, do not use single
'
quotes but use"
instead; most Windows command utilities considerthe single'
quotes part of the command-line argument! - file and directory globs are best specified with option
-g/GLOB
instead ofthe usualGLOB
command line arguments to select files and directories tosearch, especially for recursive searches; - when specifying an empty pattern
""
to match all input, this may be ignoredby some Windows command interpreters such as Powershell, in that case youmust specify option--match
instead; - to match newlines in patterns, you may want to use
\R
instead of\n
tomatch any Unicode newlines, such as\r\n
pairs and single\r
and\n
.
$ apk add ugrep ugrep-doc
Checkhttps://pkgs.alpinelinux.org/packages?name=ugrep for version info.
$ pacman -S ugrep
Checkhttps://archlinux.org/packages/extra/x86_64/ugrep for version info.
First enable theEPEL repository,then you can install ugrep.
$ dnf install ugrep
Checkhttps://packages.fedoraproject.org/pkgs/ugrep/ugrep/ for version info.
$ apt-get install ugrep
Checkhttps://packages.debian.org/ugrep for version info. To build and tryugrep
locally, see "All platforms" build steps further below.
$ dnf install ugrep
Checkhttps://packages.fedoraproject.org/pkgs/ugrep/ugrep/ for version info.
$ pkg install ugrep
Checkhttps://www.freshports.org/textproc/ugrep for version info.
$ pkgman install cmd:ugrep
Checkhttps://github.com/haikuports/haikuports/tree/master/app-text/ugrep forversion info. To build and tryugrep
locally, see "All platforms" buildsteps further below.
You can use the standard NetBSD package installer (pkgsrc):http://cdn.netbsd.org/pub/pkgsrc/current/pkgsrc/textproc/ugrep/README.html
$ pkg_add ugrep
Checkhttps://openports.pl/path/sysutils/ugrep for version info.
$ zypper install ugrep
Checkhttps://build.opensuse.org/package/show/utilities/ugrep for version info.
First enable theEPEL repository,then you can install ugrep.
$ dnf install ugrep
Checkhttps://packages.fedoraproject.org/pkgs/ugrep/ugrep/ for version info.
Cloneugrep
with
$ git clone https://github.com/Genivia/ugrep
Or visithttps://github.com/Genivia/ugrep/releases to download a specific release.
You can always add these later, when you need these features:
Option
-P
(Perl regular expressions) requires either the PCRE2 library(recommended) or the Boost.Regex library (optional fallback). If PCRE2 isnot installed, install PCRE2 with e.g.sudo apt-get install -y libpcre2-dev
ordownload PCRE2 and follow the installationinstructions. Alternatively,download Boost.Regex and run./bootstrap.sh
andsudo ./b2 --with-regex install
. SeeBoost: getting started.Option
-z
(compressed files and archives search) requires thezlib library installed. It is installed on mostsystems. If not, install it, e.g. withsudo apt-get install -y libz-dev
.To search.bz
and.bz2
files, install thebzip2 library (recommended), e.g. withsudo apt-get install -y libbz2-dev
. To search.lzma
and.xz
files,install thelzma library (recommended), e.g. withsudo apt-get install -y liblzma-dev
. To search.lz4
files, install thelz4 library (optional, not required), e.g.withsudo apt-get install -y liblz4-dev
. To search.zst
files, installthezstd library (optional, not required),e.g. withsudo apt-get install -y libzstd-dev
. To search.br
files,install thebrotli library (optional, notrequired), e.g. withsudo apt-get install -y libbrotli-dev
. To search.bz3
files, install thebzip3library (optional, not required), e.g. withsudo apt-get install -y bzip3
.
Tip
Even if your system has command line utilities, such asbzip2
, thatdoes not necessarily mean that the development libraries such aslibbz2
areinstalled. Thedevelopment libraries should be installed.
Some Linux systems may not be configured to load dynamic libraries from/usr/local/lib
, causing a library load error when runningugrep
. Tocorrect this, addexport LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/lib"
to your~/.bashrc
file. Or runsudo ldconfig /usr/local/lib
.
Execute the./build.sh
script to buildugrep
:
$ cd ugrep$ ./build.sh
This builds theugrep
executable in theugrep/src
directory with./configure
andmake -j
, verified withmake test
. When all tests pass,theugrep
executable is copied tougrep/bin/ugrep
and the symlinkugrep/bin/ug -> ugrep/bin/ugrep
is added for theug
command.
Note thatug
is the same asugrep
but also loads the configuration file.ugrep when present in the working directory or home directory. This meansthat you can define your default options forug
in .ugrep.
Alternative paths to installed or local libraries may be specified with./build.sh
. To get help on the available build options:
$ ./build.sh --help
You can build static executables by specifying:
$ ./build.sh --enable-static
This may fail if libraries don't link statically, such as brotli. In that casetry./build.sh --enable-static --without-brotli
.
You can buildugrep
with customized defaults enabled, such as a pager:
$ ./build.sh --enable-pager
Options to select defaults for builds include:
--help
display build options--enable-static
build static executables, if possible--enable-hidden
always search hidden files and directories--enable-pager
always use a pager to display output on terminals--enable-pretty
colorize output to terminals and add filename headings--disable-auto-color
disable automatic colors, requires ugrep option--color=auto
to show colors--disable-mmap
disable memory mapped files--disable-sse2
disable SSE2 and AVX optimizations--disable-avx2
disable AVX2 and AVX512BW optimizations, but compile with SSE2 when supported--disable-neon
disable ARM NEON/AArch64 optimizations--with-grep-path
the default-f
path ifGREP_PATH
is not defined--with-grep-colors
the default colors ifGREP_COLORS
is not defined
After the build completes, copyugrep/bin/ugrep
andugrep/bin/ug
to aconvenient location, for example in your~/bin
directory. Or, if you may wantto install theugrep
andug
commands and man pages:
$ sudo make install
This also installs the pattern files with predefined patterns for option-f
at/usr/local/share/ugrep/patterns/
. Option-f
first checks the workingdirectory for the presence of pattern files, if not found checks environmentvariableGREP_PATH
to load the pattern files, and if not found reads theinstalled predefined pattern files.
Unfortunately, git clones do not preserve timestamps which means that you mayrun into "WARNING: 'aclocal-1.15' is missing on your system." or thatautoheader was not found when runningmake
.
To work around this problem, run:
$ autoreconf -fi$ ./build.sh
GCC 8 and greater may produce warnings of the sort"note: parameter passingfor argument ... changed in GCC 7.1". These warnings should be ignored.
A Dockerfile is included to buildugrep
in a Ubuntu container.
Developers may want to use sanitizers to verify theugrep code when makingsignificant changes, for example to detect data races with theThreadSanitizer:
$ ./build.sh CXXFLAGS='-fsanitize=thread -O1 -g'
We checkedugrep
with the clang AddressSanitizer, MemorySanitizer,ThreadSanitizer, and UndefinedBehaviorSanitizer. These options incursignificant runtime overhead and should not be used for the final build.
Please note that theugrep andug commands search binary files bydefault and do not ignore .gitignore specified files, which will not makerecursive search performance comparisons meaningful unless options-I
and--ignore-files
are used. To make these options the default forug,simply addignore-binary
andignore-files
to your .ugrep configurationfile.
For an up-to-date performance comparison of the latest ugrep, please see theugrep performance benchmarks.Ugrep is faster than GNU grep, Silver Searcher, ack, sift. Ugrep's speed beatsripgrep in most benchmarks.
First, let's define the:grep
command in Vim to search files recursively. Todo so, add the following lines to your.vimrc
located in the root directory:
if executable('ugrep') set grepprg=ugrep\ -RInk\ -j\ -u\ --tabs=1\ --ignore-files set grepformat=%f:%l:%c:%m,%f+%l+%c+%m,%-G%f\\\|%l\\\|%c\\\|%mendif
This specifies-j
case insensitive searches with the Vim:grep
command. For case sensitive searches, remove\ -j
fromgrepprg
. Multiplematches on the same line are listed in the quickfix window separately. If thisis not desired, remove\ -u
fromgrepprg
. With this change, only the firstmatch on a line is shown. Option--ignore-files
skips files specified in.gitignore
files, when present. To limit the depth of recursive searches tothe current directory only, append\ -1
togrepprg
.
You can now invoke the Vim:grep
command in Vim to search files on aspecifiedPATH
forPATTERN
matches:
:grep PATTERN [PATH]
If you omitPATH
, then the working directory is searched. Use%
asPATH
to search only the currently opened file in Vim:
:grep PATTERN %
The:grep
command shows the results in aquickfix windowthat allows you to quickly jump to the matches found.
To open a quickfix window with the latest list of matches:
:copen
Double-click on a line in this window (or select a line and press ENTER) tojump to the file and location in the file of the match. Enter commands:cn
and:cp
to jump to the next or previous match, respectively. To update thesearch results in the quickfix window, just grep them. For example, torecursively search C++ source code markedFIXME
in the working directory:
:grep -tc++ FIXME
To close the quickfix window:
:cclose
You can useugrep options with the:grep
command, for example toselect single- and multi-line comments in the current file:
:grep -f c++/comments %
Only the first line of a multi-line comment is shown in quickfix, to savespace. To show all lines of a multi-line match, remove%-G
fromgrepformat
.
A popular Vim tool isctrlp.vim, which isinstalled with:
$ cd ~/.vim$ git clone https://github.com/kien/ctrlp.vim.git bundle/ctrlp.vim
CtrlP usesugrep by adding the following lines to your.vimrc
:
if executable('ugrep') set runtimepath^=~/.vim/bundle/ctrlp.vim let g:ctrlp_match_window='bottom,order:ttb' let g:ctrlp_user_command='ugrep "" %s -Rl -I --ignore-files -3'endif
where-I
skips binary files, option--ignore-files
skips files specified in.gitignore
files, when present, and option-3
restricts searchingdirectories to three levels (the working directory and up to two levels below).
Start Vim then enter the command:
:helptags ~/.vim/bundle/ctrlp.vim/doc
To view the CtrlP documentation in Vim, enter the command:
:help ctrlp.txt
Thanks toManuel Uberti,you can now useugrep in Emacs. To useugrep instead of GNU grepwithin Emacs, add the following line to your.emacs.d/init.el
file:
(setq-default xref-search-program ‘ugrep)
This means that Emacs commands such asproject-find-regexp
that rely onXref cannow leverage the power ofugrep.
Furthermore, it is possible to usegrep
in theEmacs grepcommands.For instance, you can runlgrep
withugrep
by customizinggrep-template
to something like the following:
(setq-default grep-template "ugrep --color=always -0Iinr -e <R>")
If you do not have Emacs version 29 (or greater) you can download and buildEmacs from theEmacs master branch,or enable Xref integration withugrep manually:
(with-eval-after-load 'xref (push '(ugrep . "xargs -0 ugrep <C> --null -ns -e <R>") xref-search-program-alist) (setq-default xref-search-program 'ugrep))
ugrep supports all standard GNU/BSD grep command-line options and improvesmany of them too. Seenotable improvements over grep.
In fact, executingugrep
with options-U
,-Y
,-.
and--sort
makes itbehave likeegrep
, permitting empty patterns to match and search hidden filesinstead of ignoring them. Seegrep equivalence.
You can createconvenient grep aliases with or without options
-Y
,-.
and--sort
or include other options as desired. If you reallymust stick exactly to GNU/BSD grep ASCII/LATIN1 patterns, use options-U
and--grep
to disable Unicode pattern matching and to reassign options-z
and-Z
to--null-data
and--null
, respectively.You can also create
grep
,egrep
andfgrep
executables by symlinking orcopyingugrep
to those names. When theugrep
(orugrep.exe
) executableis copied asgrep
(grep.exe
),egrep
(egrep.exe
),fgrep
(fgrep.exe
), then options-Y
and-.
are automatically enabled togetherwith either-G
forgrep
,-E
foregrep
and-F
forfgrep
. Inaddition, when copied aszgrep
,zegrep
andzfgrep
, option--decompress
is enabled. For example, whenugrep
is copied aszegrep
,options--decompress
,-E
,-Y
,-.
and--sort
are enabled.Likewise, symlinks and hard links can be used to create
grep
,egrep
andfgrep
replacements in the usual installation directories. For example:sudo ln -s `which ugrep` /opt/local/bin/grepsudo ln -s `which ugrep` /opt/local/bin/egrepsudo ln -s `which ugrep` /opt/local/bin/fgrepsudo ln -s `which ugrep` /opt/local/bin/zgrepsudo ln -s `which ugrep` /opt/local/bin/zegrepsudo ln -s `which ugrep` /opt/local/bin/zfgrep
The
/opt/local/bin
here is an example and may or may not be in your$path
and may or may not be found, so please adjust as necessary.Caution:bash does not obey the linked name when executing the program, reverting tothe nameugrep
instead, which negates all internal compatibility settings.To avoid this, copy the executables instead of linking!
When linking or copyingugrep
togrep
,egrep
,fgrep
,zgrep
,zegrep
,zfgrep
, options-z
and-Z
are reassigned for compatibility to GNU/BSDgrep options--null-data
and--null
, respectively.
When theugrep
executable file is symlinked or copied togrep
,egrep
,fgrep
,zgrep
,zegrep
andzfgrep
executables, then those executableswill behave as GNU grep equivalents. This behavior is implicit and automatic,essentially using the following translations:
grep = ugrep -G -Y -. --sortegrep = ugrep -E -Y -. --sortfgrep = ugrep -F -Y -. --sortzgrep = ugrep -z -G -Y -. --sortzegrep = ugrep -z -E -Y -. --sortzfgrep = ugrep -z -F -Y -. --sort
please note that:
-Y
enables empty matches, so for example the patterna*
matches everyline instead of a sequence ofa
's. By default in ugrep, the patterna*
matches a sequence ofa
's. Moreover, in ugrep the patterna*b*c*
matcheswhat it is supposed to match by default. Seeimprovements.-.
searches hidden files (dotfiles). By default, hidden files are ignored,like most Unix utilities.--sort
specifies output sorted by pathname, showing sorted matching filesfirst followed by sorted recursive matches in subdirectories. Otherwise,matching files are reported in no particular order to improve performance;- options
-z
and-Z
are reassigned to--null-data
and--null
and nolonger enable--decompress
and--fuzzy
searching modes.
There is one minor difference with GNU/BSD grep:
- GNU/BSD grep defaults to
-Dread
and-dread
which are not recommended, seeimprovements for an explanation.
Commonly-used aliases to add to.bashrc
to increase productivity:
alias uq = 'ug -Q' # interactive TUI search (uses .ugrep config)alias uz = 'ug -z' # compressed files and archives search (uses .ugrep config)alias ux = 'ug -U --hexdump' # binary pattern search (uses .ugrep config)alias ugit = 'ug -R --ignore-files' # works like git-grep & define your preferences in .ugrep configalias grep = 'ug -G' # search with basic regular expressions (BRE) like grepalias egrep = 'ug -E' # search with extended regular expressions (ERE) like egrepalias fgrep = 'ug -F' # find string(s) like fgrepalias zgrep = 'ug -zG' # search compressed files and archives with BREalias zegrep = 'ug -zE' # search compressed files and archives with EREalias zfgrep = 'ug -zF' # find string(s) in compressed files and/or archivesalias xdump = 'ugrep -X ""' # hexdump files without searching (don't use .ugrep config)alias zmore = 'ugrep+ -z -I -+ --pager ""' # view compressed, archived and regular files (don't use .ugrep config)
- ugrep starts an interactive query TUI with option
-Q
. - ugrep matches patterns across multiple lines when patterns match
\n
. - ugrep matches full Unicode by default (disabled with option
-U
). - ugrep supports Boolean patterns with AND, OR and NOT (option
--bool
). - ugrep supports gitignore with option
--ignore-files
. - ugrep supports fuzzy (approximate) matching with option
-Z
. - ugrep supports user-defined global and local configuration files.
- ugrep searches compressed files and archives with option
-z
. - ugrep searches cpio, jar, pax, tar, zip and 7z archives with option
-z
. - ugrep searches cpio, jar, pax, tar, zip and 7z archives recursivelystored within archives with
-z
and--zmax=NUM
for up toNUM
levels deep. - ugrep searches pdf, doc, docx, xls, xlsx, epub, and more with
--filter
using third-party format conversion utilities as plugins. - ugrep searches a directory when the FILE argument is a directory, likemost Unix/Linux utilities; use option
-r
to search directories recursively. - ugrep does not match hidden files by default like most Unix/Linuxutilities (hidden dotfile file matching is enabled with
-.
). - ugrep regular expression patterns are more expressive than GNU grep andBSD grep POSIX ERE and support Unicode pattern matching. Extended regularexpression (ERE) syntax is the default (i.e. option
-E
as egrep, whereas-G
enables BRE). - ugrep spawns threads to search files concurrently to improve searchspeed (disabled with option
-J1
). - ugrep produces hexdumps with
-W
(output binary matches in hex with textmatches output as usual) and-X
(output all matches in hex). - ugrep can output matches in JSON, XML, CSV and user-defined formats (withoption
--format
). - ugrep option
-f
usesGREP_PATH
environment variable or the predefinedpatterns installed in/usr/local/share/ugrep/patterns
. If-f
isspecified and also one or more-e
patterns are specified, then options-F
,-x
, and-w
do not apply to-f
patterns. This is to avoidconfusion when-f
is used with predefined patterns that may no longer workproperly with these options. - ugrep options
-O
,-M
, and-t
specify file extensions, filesignature magic byte patterns, and predefined file types, respectively. Thisallows searching for certain types of files in directory trees, for examplewith recursive search options-R
and-r
. Options-O
,-M
, and-t
also applies to archived files in cpio, jar, pax, tar, zip and 7z files. - ugrep option
-k
,--column-number
to display the column number, takingtab spacing into account by expanding tabs, as specified by option--tabs
. - ugrep option
-P
(Perl regular expressions) supports backreferences(with--format
) and lookbehinds, which uses the PCRE2 or Boost.Regexlibrary for fast Perl regex matching with a PCRE-like syntax. - ugrep option
-b
with option-o
or with option-u
, ugrep displaysthe exact byte offset of the pattern match instead of the byte offset of thestart of the matched line reported by GNU/BSD grep. - ugrep option
-u
,--ungroup
to not group multiple matches per line.This option displays a matched input line again for each additional patternmatch on the line. This option is particularly useful with option-c
toreport the total number of pattern matches per file instead of the number oflines matched per file. - ugrep option
-Y
enables matching empty patterns. Grepping withempty-matching patterns is weird and gives different results with GNU grepversus BSD grep. Empty matches are not output byugrep by default, whichavoids making mistakes that may produce "random" results. For example, withGNU/BSD grep, patterna*
matches every line in the input, and actuallymatchesxyz
three times (the empty transitions before and between thex
,y
, andz
). Allowing empty matches requiresugrep option-Y
.Patterns that start with^
or end with$
, such as^\h*$
, match empty.These patterns automatically enable option-Y
. - ugrep option
-D, --devices=ACTION
isskip
by default, instead ofread
. This prevents unexpectedly hanging on named pipes in directoriesthat are recursively searched, as may happen with GNU/BSD grep thatread
devices by default. - ugrep option
-d, --directories=ACTION
isskip
by default, instead ofread
. By default, directories specified on the command line are searched,but not recursively deeper into subdirectories. - ugrep offersnegative patterns
-N PATTERN
, which are patterns of theform(?^X)
that skip allX
input, thus removingX
from the search.For example, negative patterns can be used to skip strings and comments whensearching for identifiers in source code and find matches that aren't instrings and comments. Predefinedzap
patterns use negative patterns, forexample, use-f cpp/zap_comments
to ignore pattern matches in C++ comments. - ugrep ignores the
GREP_OPTIONS
environment variable, because thebehavior ofugrep must be portable and predictable on every system. AlsoGNU grep abandonedGREP_OPTIONS
for this reason. Please use theug
command that loads the .ugrep configuration file located in the workingdirectory or in the home directory when present, or use shell aliases tocreate new commands with specific search options.
To perform a search using a configuration file.ugrep
placed in the workingdirectory or home directory (note thatug
is the same asugrep --config
):
ug PATTERN FILE...
To save a.ugrep
configuration file to the working directory, then edit thisfile in your home directory to customize your preferences forug
defaults:
ug --save-config
To search the working directory and recursively deeper formain
(note that-r
recurse symlinks is enabled by default if no file arguments arespecified):
ug main
Same, but only search C++ source code files recursively, ignoring all otherfiles:
ug -tc++ main
Same, using the interactive query TUI, starting with the initial search patternmain
(note that-Q
with an initial pattern requires option-e
becausepatterns are normally specified interactively and all command line argumentsare considered files/directories):
ug -Q -tc++ -e main
To search for#define
(and# define
etc) using a regex pattern in C++ files(note that patterns should be quoted to prevent shell globbing of*
and?
):
ug -tc++ '#[\t ]*define'
To search formain
as a word (-w
) recursively without following symlinks(-r
) in directorymyproject
, showing the matching line (-n
) and column(-k
) numbers next to the lines matched:
ug -r -nkw main myproject
Same, but only searchmyproject
without recursing deeper (note that directoryarguments are searched at one level by default):
ug -nkw main myproject
Same, but searchmyproject
and one subdirectory level deeper (two levels)with-2
:
ug -2 -nkw main myproject
Same, but only search C++ files inmyproject
and its subdirectories with-tc++
:
ug -tc++ -2 -nkw main myproject
Same, but also search inside archives (e.g. zip and tar files) and compressedfiles with-z
:
ug -z -tc++ -2 -nkw main myproject
Search recursively the working directory formain
while ignoring gitignoredfiles (e.g. assuming.gitignore
is in the working directory or below):
ug --ignore-files -tc++ -nkw main
To list all files in the working directory and deeper that are not ignored by.gitignore
file(s):
ug --ignore-files -l ''
To display the list of file name extensions and "magic bytes" (shebangs)that are searched corresponding to-t
arguments:
ug -tlist
To list all shell files recursively, based on extensions and shebangs with-l
(note that''
matches any non-empty file):
ug -l -tShell ''
To search formain
in source code while ignoring strings and comment blocksyou can usenegative patterns with option-N
to skip unwanted matches inC/C++ quoted strings and comment blocks:
ug -r -nkw -e 'main' -N '"(\\.|\\\r?\n|[^\\\n"])*"|//.*|/\*(.*\n)*?.*\*+\/' myproject
This is a lot of work to type in correctly! If you are like me, I don't wantto spend time fiddling with regex patterns when I am working on something moreimportant. There is an easier way by usingugrep's predefined patterns(-f
) that are installed with theugrep
tool:
ug -r -nkw 'main' -f c/zap_strings -f c/zap_comments myproject
This query also searches through other files than C/C++ source code, likeREADMEs, Makefiles, and so on. We're also skipping symlinks with-r
. Solet's refine this query by selecting C/C++ files only using option-tc,c++
and include symlinks to files and directories with-R
:
ug -R -tc,c++ -nkw 'main' -f c/zap_strings -f c/zap_comments myproject
What if you only want to look for the identifiermain
but not as a functionmain(
? In this case, use a negative pattern for this to skip unwantedmain\h*(
pattern matches:
ug -R -tc,c++ -nkw -e 'main' -N 'main\h*\(' -f c/zap_strings -f c/zap_comments myproject
This uses the-e
and-N
options to explicitly specify a pattern and anegative pattern, respectively, which is essentially forming the patternmain|(?^main\h*\()
, where\h
matches space and tab. In general, negativepatterns are useful to filter out pattern matches that we are not interestedin.
As another example, let's say we may want to search for the wordFIXME
inC/C++ comment blocks. To do so we can first select the comment blocks withugrep's predefinedc/comments
pattern AND THEN select lines withFIXME
using a pipe:
ug -R -tc,c++ -nk -f c/comments myproject | ug -w 'FIXME'
Filtering results with pipes is generally easier than using AND-OR logic thatsome search tools use. This approach follows the Unix spirit to keep utilitiessimple and use them in combination for more complex tasks.
Let's produce a sorted list of all identifiers found in Java source code whileskipping strings and comments:
ug -R -tjava -f java/names myproject | sort -u
This matches Java Unicode identifiers using the regex\p{JavaIdentifierStart}\p{JavaIdentifierPart}*
defined inpatterns/java/names
.
With traditional grep and grep-like tools it takes great effort to recursivelysearch for the C/C++ source file that defines functionqsort
, requiringsomething like this:
ug -R --include='*.c' --include='*.cpp' '^([ \t]*[[:word:]:*&]+)+[ \t]+qsort[ \t]*\([^;\n]+$' myproject
Fortunately, withugrep we can simply select all function definitions infiles with extension.c
or.cpp
by using option-Oc,cpp
and by using apredefined patternfunctions
that is installed with the tool to produceall function definitions. Then we select the one we want:
ug -R -Oc,cpp -nk -f c/functions | ug 'qsort'
Note that we could have used-tc,c++
to select C/C++ files, but this alsoincludes header files when we want to only search.c
and.cpp
files.
We can also skip files and directories from being searched that are defined in.gitignore
. To do so we use--ignore-files
to exclude any files anddirectories from recursive searches that match the globs in.gitignore
, whenone or more.gitignore
files are found:
ug -R -tc++ --ignore-files -f c++/defines
This searches C++ files (-tc++
) in the working directory for#define
lines (-f c++/defines
), while skipping files and directories declared in.gitignore
. If you find this too long to type then define an alias to searchGitHub directories:
alias ugit='ugrep -R --ignore-files'ugit -tc++ -f c++/defines
To highlight matches when pushed through a chain of pipes we should use--color=always
:
ugit --color=always -tc++ -f c++/defines | ugrep -w 'FOO.*'
This returns a color-highlighted list of all#define FOO...
macros in C/C++source code files, skipping files defined in.gitignore
.
Note that the complement of--exclude
is not--include
, because exclusionsalways take precedence over inclusions, so we cannot reliably list the filesthat are ignored with--include-from='.gitignore'
. Only files explicitlyspecified with--include
and directories explicitly specified with--include-dir
are visited. The--include-from
from lists globs that areconsidered both files and directories to add to--include
and--include-dir
, respectively. This means that when directory names anddirectory paths are not explicitly listed in this file then it will not bevisited using--include-from
.
Because ugrep checks if the input is valid UTF-encoded Unicode (unless-U
isused), it is possible to use it as a filter to ignore non-UTF output producedby a program:
program | ugrep -I ''
If the program produces valid output then the output is passed through,otherwise the output is filtered out option-I
. If the output is initiallyvalid for a very large portion but is followed by invalid output, then ugrepmay initially show the output up to but excluding the invalid output afterwhich further output is blocked.
To filter lines that are valid ASCII or UTF-encoded, while removing lines thatare not:
program | ugrep '[\p{Unicode}--[\n]]+'
Note that\p{Unicode}
matches\n
but we don't want to matche the wholefile! Just lines with[\p{Unicode}--[\n]]+
.
The ugrep man page:
man ugrep
To show a help page:
ug --help
To show options that mentionWHAT
:
ug --help WHAT
To show a list of-t TYPES
option values:
ug -tlist
In the interactive query TUI, press F1 or CTRL-Z for help and options:
ug -Q
--config[=FILE], ---[FILE] Use configuration FILE. The default FILE is `.ugrep'. The working directory is checked first for FILE, then the home directory. The options specified in the configuration FILE are parsed first, followed by the remaining options specified on the command line. The ug command automatically loads a `.ugrep' configuration file, unless --config=FILE or --no-config is specified.--no-config Do not load the default .ugrep configuration file.--save-config[=FILE] [OPTIONS] Save configuration FILE to include OPTIONS. Update FILE when first loaded with --config=FILE. The default FILE is `.ugrep', which is automatically loaded by the ug command. When FILE is a `-', writes the configuration to standard output. Only part of the OPTIONS are saved that do not cause searches to fail when combined with other options. Additional options may be specified by editing the saved configuration file. A configuration file may be modified manually to specify one or more config[=FILE] to indirectly load the specified FILEs, but recursive config loading is not allowed.
Theug
command is intended for context-dependent interactive searching and isequivalent to theugrep --config
command to load the configuration file.ugrep
when present in the working directory or, when not found, in the homedirectory:
ug PATTERN ...ugrep --config PATTERN ...
Theug
command also sorts files by name per directory searched. Aconfiguration file containsNAME=VALUE
pairs per line, whereNAME
is thename of a long option (without--
) and=VALUE
is an argument, which isoptional and may be omitted depending on the option. Empty lines and linesstarting with a#
are ignored:
# Color schemecolors=cx=hb:ms=hiy:mc=hic:fn=hi+y+K:ln=hg:cn=hg:bn=hg:se=# Disable searching hidden files and directoriesno-hidden# ignore files specified in .ignore and .gitignore in recursive searchesignore-files=.ignoreignore-files=.gitignore
Command line options are parsed in the following order: first the (default ornamed) configuration file is loaded, then the remaining options andarguments on the command line are parsed.
Option--stats
displays the configuration file used after searching.
Named configuration files are intended to streamline custom search tasks, byreducing the number of command line options to just one---FILE
to use thecollection of options specified inFILE
. The--config=FILE
option and itsabbreviated form---FILE
load the specified configuration file located in theworking directory or, when not found, located in the home directory:
ug ---FILE PATTERN ...ugrep ---FILE PATTERN ...
An error is produced whenFILE
is not found or cannot be read.
Named configuration files can be used to define a collection of options thatare specific to the requirements of a task in the development workflow of aproject. For example to report unresolved issues by checking the source codeand documentation for comments with FIXME and TODO items. Such namedconfiguration file can be localized to a project by placing it in the projectdirectory, or it can be made global by placing it in the home directory. Forvisual feedback, a color scheme specific to this task can be specified withoptioncolors
in the configurationFILE
to help identify the outputproduced by a named configuration as opposed to the default configuration.
The--save-config
option saves a.ugrep
configuration file to the workingdirectory using the current configuration loaded with--config
. This savesthe current configuration combined with additional options when specified also.Only those options that cannot conflict with other options and options thatcannot negatively impact search results will be saved.
The--save-config=FILE
option saves the configuration to the specifiedFILE
.The configuration is written to standard output whenFILE
is a-
.
Alternatively, a configuration file may be manually created or modified. Aconfiguration file may include one or moreconfig[=FILE]
to indirectly loadthe specfifiedFILE
, but recursive config loading is prohibited. Thesimplest way to manuall create a configuration file is to specifyconfig
atthe top of the file, followed by the long options to override the defaults.
-Q[=DELAY], --query[=DELAY] Query mode: start a TUI to perform interactive searches. This mode requires an ANSI capable terminal. An optional DELAY argument may be specified to reduce or increase the response time to execute searches after the last key press, in increments of 100ms, where the default is 3 (300ms delay). No whitespace may be given between -Q and its argument DELAY. Initial patterns may be specified with -e PATTERN, i.e. a PATTERN argument requires option -e. Press F1 or CTRL-Z to view the help screen. Press F2 or CTRL-Y to invoke a command to view or edit the file shown at the top of the screen. The command can be specified with option --view, or defaults to environment variable PAGER when defined, or EDITOR. Press Tab and Shift-Tab to navigate directories and to select a file to search. Press Enter to select lines to output. Press ALT-l for option -l to list files, ALT-n for -n, etc. Non-option commands include ALT-] to increase context. See also options --no-confirm, --delay, --split and --view.--no-confirm Do not confirm actions in -Q query TUI. The default is confirm.--delay=DELAY Set the default -Q key response delay. Default is 3 for 300ms.--split Split the -Q query TUI screen on startup.--view[=COMMAND] Use COMMAND to view/edit a file in -Q query TUI by pressing CTRL-Y.
This option starts a user interface to enter search patterns interactively:
- Press F1 or CTRL-Z to view a help screen and to enable or disable options.
- Press Alt with a key corresponding to a ugrep option letter or digit toenable or disable the ugrep option. For example, pressing Alt-c enablesoption
-c
to count matches. Pressing Alt-c again disables-c
. Optionscan be toggled with the Alt key while searching or when viewing the helpscreen. If Alt/Meta keys are not working (e.g. X11 xterm), then pressCTRL-O followed by the key corresponding to the option. Alt keys may workin xterm by addingxterm*metaSendsEscape: true
to ~/.Xdefaults`. - Press Alt-g to enter or edit option
-g
file and directory matching globs, acomma-separated list of gitignore-style glob patterns. Presssing ESC returnscontrol to the query pattern prompt (the globs are saved). When a glob ispreceded by a!
or a^
, skips files whose name matches the glob When aglob contains a/
, full pathnames are matched. Otherwise basenames arematched. When a glob ends with a/
, directories are matched. - The query TUI prompt switches between
Q>
(normal),F>
(fixed strings),G>
(basic regex),P>
(Perl matching), andZ>
(fuzzy matching).When the--glob=
prompt is shown, a comma-separated list of gitignore-styleglob patterns may be entered. Presssing ESC returns control to the patternprompt. - Press CTRL-T to split the TUI screen to preview a file in the bottom pane.
- Press CTRL-Y to view a file with a pager specified with
--view
. - Press Enter to switch to selection mode to select lines to output when ugrepexits. Normally, ugrep in query mode does not output any results unlessresults are selected. While in selection mode, select or deselect lines withEnter or Del, or press A to select all results.
- The file listed or shown at the top of the screen, or beneath the cursor inselection mode, is edited by pressing F2 or CTRL-Y. A file viewer or editormay be specified with
--view=COMMAND
. Otherwise, thePAGER
orEDITOR
environment variables are used to invoke the command with CTRL-Y. Filenamesmust be enabled and visible in the output to use this feature. - Press TAB to chdir one level down into the directory of the file listedor viewed at the top of the screen. If no directory exists, the file itselfis selected to search. Press Shift-TAB to go back up one level.
- Press CTRL-] to toggle colors on and off. Normally ugrep in query mode usescolors and other markup to highlight results. When colors are turned off,selected results are also not colored in the output produced by ugrep whenugrep exits. When colors are turned on (the default), selected results arecolored depending on the
--color
option. - The query engine is optimized to limit system load by performing on-demandsearches to produce results only for the visible parts shown in theinterface. That is, results are shown on demand, when scrolling down andwhen exiting when all results are selected. When the search pattern ismodified, the previous search query is cancelled when incomplete. Thiseffectively limits the load on the system to maintain a high degree ofresponsiveness of the query engine to user input. Because the search resultsare produced on demand, occasionally you may notice a flashing "Searching..."message when searching files.
- To display results faster, specify a low
DELAY
value such as 1. However,lower values may increase system load as a result of repeatedly initiatingand cancelling searches by each key pressed. - To avoid long pathnames to obscure the view,
--heading
is enabled bydefault. Press Alt-+ to switch headings off.
Query TUI key mapping:
key(s) | function |
---|---|
Alt-key | toggle ugrep command-line option corresponding tokey |
Alt-/ xxxx/ | insert Unicode hex code point U+xxxx |
Esc Ctrl-C | go back or exit |
Ctrl-Q | quick exit and output the results selected in selection mode |
Tab | chdir to the directory of the file shown at the top of the screen or select file |
Shift-Tab | chdir one level up or deselect file |
Enter | enter selection mode and toggle selected lines to output on exit |
Up Ctrl-P | move up |
Down Ctrl-N | move down |
Left Ctrl-B | move left |
Right Ctrl-F | move right |
PgUp Ctrl-G | move display up by a page |
PgDn Ctrl-D | move display down by a page |
Alt-Up | move display up by 1/2 page (MacOSShift-Up ) |
Alt-Down | move display down by 1/2 page (MacOSShift-Down ) |
Alt-Left | move display left by 1/2 page (MacOSShift-Left ) |
Alt-Right | move display right by 1/2 page (MacOSShift-Right ) |
Home Ctrl-A | move cursor to the beginning of the line |
End Ctrl-E | move cursor to the end of the line |
Ctrl-K | delete after cursor |
Ctrl-L | refresh screen |
Ctrl-O +key | toggle ugrep command-line option corresponding tokey , same asAlt-key |
Ctrl-R F4 | jump to bookmark |
Ctrl-S | jump to the next dir/file/context |
Ctrl-T F5 | toggle split screen (--split starts a split-screen TUI) |
Ctrl-U | delete before cursor |
Ctrl-V | verbatim character |
Ctrl-W | jump back one dir/file/context |
Ctrl-X F3 | set bookmark |
Ctrl-Y F2 | view or edit the file shown at the top of the screen |
Ctrl-Z F1 | view help and options |
Ctrl-^ | chdir back to the starting working directory |
Ctrl-] | toggle color/mono |
Ctrl-\ | terminate process |
To interactively search the files in the working directory and below:
ug -Q
Same, but restricted to C++ files only and ignoring.gitignore
files:
ug -Q -tc++ --ignore-files
To interactively search all makefiles in the working directory and below:
ug -Q -g 'Makefile*' -g 'makefile*'
Same, but for up to 2 directory levels (working and one subdirectory level):
ug -Q -2 -g 'Makefile*' -g 'makefile*'
To interactively view the contents ofmain.cpp
and search it, where-y
shows any nonmatching lines as context:
ug -Q -y main.cpp
To interactively searchmain.cpp
, starting with the search patternTODO
anda match context of 5 lines (context can be interactively enabled and disabled,this also overrides the default context size of 2 lines):
ug -Q -C5 -e TODO main.cpp
To view and search the contents of an archive (e.g. zip, tarball):
ug -Q -z archive.tar.gz
To interactively select files fromproject.zip
to decompress withunzip
,using ugrep query selection mode (press Enter to select lines):
unzip project.zip `zipinfo -1 project.zip | ugrep -Q`
-L, --files-without-match Only the names of files not containing selected lines are written to standard output. Pathnames are listed once per file searched. If the standard input is searched, the string ``(standard input)'' is written.-l, --files-with-matches Only the names of files containing selected lines are written to standard output. ugrep will only search a file until a match has been found, making searches potentially less expensive. Pathnames are listed once per file searched. If the standard input is searched, the string ``(standard input)'' is written.-R, --dereference-recursive Recursively read all files under each directory. Follow all symbolic links to files and directories, unlike -r.-r, --recursive Recursively read all files under each directory, following symbolic links only if they are on the command line. Note that when no FILE arguments are specified and input is read from a terminal, recursive searches are performed as if -r is specified.-S, --dereference-files When -r is specified, symbolic links to files are followed, but not to directories. The default is not to follow symbolic links.--depth=[MIN,][MAX], -1, -2, -3, ... -9, -10, -11, -12, ... Restrict recursive searches from MIN to MAX directory levels deep, where -1 (--depth=1) searches the specified path without recursing into subdirectories. Note that -3 -5, -3-5, and -35 search 3 to 5 levels deep. Enables -r if -R or -r is not specified.-g GLOBS, --glob=GLOBS Search only files whose name matches the specified comma-separated list of GLOBS, same as --include='glob' for each `glob' in GLOBS. When a `glob' is preceded by a `!' or a `^', skip files whose name matches `glob', same as --exclude='glob'. When `glob' contains a `/', full pathnames are matched. Otherwise basenames are matched. When `glob' ends with a `/', directories are matched, same as --include-dir='glob' and --exclude-dir='glob'. A leading `/' matches the working directory. This option may be repeated and may be combined with options -M, -O and -t to expand searches. See `ugrep --help globs' and `man ugrep' section GLOBBING for details.-O EXTENSIONS, --file-extension=EXTENSIONS Search only files whose filename extensions match the specified comma-separated list of EXTENSIONS, same as --include='*.ext' for each `ext' in EXTENSIONS. When `ext' is preceded by a `!' or a `^', skip files whose filename extensions matches `ext', same as --exclude='*.ext'. This option may be repeated and may be combined with options -g, -M and -t to expand the recursive search.-t TYPES, --file-type=TYPES Search only files associated with TYPES, a comma-separated list of file types. Each file type corresponds to a set of filename extensions passed to option -O and filenames passed to option -g. For capitalized file types, the search is expanded to include files with matching file signature magic bytes, as if passed to option -M. When a type is preceded by a `!' or a `^', excludes files of the specified type. This option may be repeated.--stats Output statistics on the number of files and directories searched, and the inclusion and exclusion constraints applied.
If no FILE arguments are specified and input is read from a terminal, recursivesearches are performed as if-r
is specified. To force reading from standardinput, specify-
as the FILE argument.
To recursively list all non-empty files in the working directory:
ug -r -l ''
To list all non-empty files in the working directory but not deeper (since aFILE argument is given, in this case.
for the working directory):
ug -l '' .
To list all non-empty files in directorymydir
but not deeper (since a FILEargument is given):
ug -l '' mydir
To list all non-empty files in directorymydir
and deeper while followingsymlinks:
ug -R -l '' mydir
To recursively list all non-empty files on the path specified, while visitingsubdirectories only, i.e. directoriesmydir/
and subdirectories at onelevel deepermydir/*/
are visited (note that-2 -l
can be abbreviated to-l2
):
ug -2 -l '' mydir
To recursively list all non-empty files in directorymydir
, not following anysymbolic links (except when on the command line such asmydir
):
ug -rl '' mydir
To recursively list all Makefiles matching the textCPP
:
ug -l -tmake 'CPP'
To recursively list allMakefile.*
matchingbin_PROGRAMS
:
ug -l -g'Makefile.*' 'bin_PROGRAMS'
To recursively list all non-empty files with extension .sh, with-Osh
:
ug -l -Osh ''
To recursively list all shell scripts based on extensions and shebangs with-tShell
:
ug -l -tShell ''
To recursively list all shell scripts based on extensions only with-tshell
:
ug -l -tshell ''
--bool, -%, -%% Specifies Boolean query patterns. A Boolean query pattern is composed of `AND', `OR', `NOT' operators and grouping with `(' `)'. Spacing between subpatterns is the same as `AND', `|' is the same as `OR' and a `-' is the same as `NOT'. The `OR' operator binds more tightly than `AND'. For example, --bool 'A|B C|D' matches lines with (`A' or `B') and (`C' or `D'), --bool 'A -B' matches lines with `A' and not `B'. Operators `AND', `OR', `NOT' require proper spacing. For example, --bool 'A OR B AND C OR D' matches lines with (`A' or `B') and (`C' or `D'), --bool 'A AND NOT B' matches lines with `A' without `B'. Quoted subpatterns are matched literally as strings. For example, --bool 'A "AND"|"OR"' matches lines with `A' and also either `AND' or `OR'. Parentheses are used for grouping. For example, --bool '(A B)|C' matches lines with `A' and `B', or lines with `C'. Note that all subpatterns in a Boolean query pattern are regular expressions, unless -F is specified. Options -E, -F, -G, -P and -Z can be combined with --bool to match subpatterns as strings or regular expressions (-E is the default.) This option does not apply to -f FILE patterns. The double short option -%% enables options --bool --files. Option --stats displays the Boolean search patterns applied. See also options --and, --andnot, --not, --files and --lines.--files Boolean file matching mode, the opposite of --lines. When combined with option --bool, matches a file if all Boolean conditions are satisfied. For example, --bool --files 'A B|C -D' matches a file if some lines match `A', and some lines match either `B' or `C', and no line matches `D'. See also options --and, --andnot, --not, --bool and --lines. The double short option -%% enables options --bool --files.--lines Boolean line matching mode for option --bool, the default mode.--and [[-e] PATTERN] ... -e PATTERN Specify additional patterns to match. Patterns must be specified with -e. Each -e PATTERN following this option is considered an alternative pattern to match, i.e. each -e is interpreted as an OR pattern. For example, -e A -e B --and -e C -e D matches lines with (`A' or `B') and (`C' or `D'). Note that multiple -e PATTERN are alternations that bind more tightly together than --and. Option --stats displays the search patterns applied. See also options --not, --andnot, and --bool.--andnot [[-e] PATTERN] ... Combines --and --not. See also options --and, --not, and --bool.--not [-e] PATTERN Specifies that PATTERN should not match. Note that -e A --not -e B matches lines with `A' or lines without a `B'. To match lines with `A' that have no `B', specify -e A --andnot -e B. Option --stats displays the search patterns applied. See also options --and, --andnot, and --bool.--stats Output statistics on the number of files and directories searched, and the inclusion and exclusion constraints applied.
Note that the--and
,--not
, and--andnot
options require-e PATTERN
.
The-%
option makes all patterns Boolean-based, supporting the followinglogical operations listed from the highest level of precedence to the lowest:
operator | alternative | result |
---|---|---|
"x" | matchx literally and exactly as specified (using the standard regex escapes\Q and\E ) | |
( ) | Boolean expression grouping | |
-x | NOT x | inverted match, i.e. matches ifx does not match |
x|y | x OR y | matches lines withx ory |
x y | x AND y | matches lines with bothx andy |
x
andy
are subpatterns that do not start with the special symbols|
,-
, and(
(use quotes or a\
escape to match these);-
andNOT
are the same and take precedence overOR
, which means that-x|y
==(-x)|y
for example.|
andOR
are the same and take precedence overAND
, which means thatx y|z
==x (y|z)
for example;
The--stats
option displays the Boolean queries in human-readable formconverted to CNF (Conjunctive Normal Form), after the search is completed.To show the CNF without a search, read from standard input terminated by anEOF, likeecho | ugrep -% '...' --stats
.
Subpatterns are color-highlighted in the output, except those negated withNOT
(aNOT
subpattern may still show up in a matching line when using anOR-NOT pattern likex|-y
). Note that subpatterns may overlap. In thatcase only the first matching subpattern is color-highlighted.
Multiple lines may be matched when subpatterns match newlines. There is oneexception however: subpatterns ending with(?=X)
lookaheads may not matchwhenX
spans multiple lines.
Empty patterns match any line (grep standard). Therefore,-% 'x|""|y'
matches everything andx
andy
are not color-highlighted. Option-y
should be used to show every line as context, for example-y 'x|y'
.
Fzf-like interactive querying (Boolean search with fixed strings with fuzzymatching to allow e.g. up to 4 extra characters matched with-Z+4
in wordswith-w
), press TAB and ALT-y to view a file with matches. Press SHIFT-TABand ALT-l to go back to the list of matching files:
ug -Q -%% -l -w -F -Z+4 --sort=best
To recursively find all files containing bothhot
anddog
anywhere in thefile with option--files
:
ug -%% 'hot dog'ug --files -e hot --and dog
To find lines containing bothhot
anddog
inmyfile.txt
:
ug -% 'hot dog' myfile.txtug -e hot --and dog myfile.txt
To find lines containingplace
and then alsohotdog
ortaco
(or both) inmyfile.txt
:
ug -% 'hotdog|taco place' myfile.txtug -e hotdog -e taco --and place myfile.txt
Same, but exclude lines matchingdiner
:
ug -% 'hotdog|taco place -diner' myfile.txtug -e hotdog -e taco --and place --andnot diner myfile.txt
To find lines withdiner
or lines that match bothfast
andfood
but notbad
inmyfile.txt
:
ug -% 'diner|(fast food -bad)' myfile.txt
To find lines withfast food
(exactly) or lines withdiner
but notbad
orold
inmyfile.txt
:
ug -% '"fast food"|diner -bad -old' myfile.txt
Same, but using a different Boolean expression that has the same meaning:
ug -% '"fast food"|diner -(bad|old)' myfile.txt
To find lines withdiner
implyinggood
inmyfile.txt
(that is, show lineswithgood
withoutdiner
and show lines withdiner
but only those withgood
, which is logically implied!):
ug -% 'good|-diner' myfile.txtug -e good --not diner myfile.txt
To find lines withfoo
and-bar
and"baz"
inmyfile.txt
(not that-
and"
should be matched using\
escapes and with--and -e -bar
):
ug -% 'foo \-bar \"baz\"' myfile.txtug -e foo --and -e -bar --and '"baz"' myfile.txt
To searchmyfile.cpp
for lines withTODO
orFIXME
but not both on thesame line, like XOR:
ug -% 'TODO|FIXME -(TODO FIXME)' myfile.cppug -e TODO -e FIXME --and --not TODO --not FIXME myfile.cpp
-e PATTERN, --regexp=PATTERN Specify a PATTERN to search the input. An input line is selected if it matches any of the specified patterns. This option is useful when multiple -e options are used to specify multiple patterns, or when a pattern begins with a dash (`-'), or to specify a pattern after option -f or after the FILE arguments.-f FILE, --file=FILE Read newline-separated patterns from FILE. White space in patterns is significant. Empty lines in FILE are ignored. If FILE does not exist, the GREP_PATH environment variable is used as path to FILE. If that fails, looks for FILE in /usr/local/share/ugrep/pattern. When FILE is a `-', standard input is read. This option may be repeated.-L, --files-without-match Only the names of files not containing selected lines are written to standard output. Pathnames are listed once per file searched. If the standard input is searched, the string ``(standard input)'' is written.-N PATTERN, --neg-regexp=PATTERN Specify a negative PATTERN to reject specific -e PATTERN matches with a counter pattern. Note that longer patterns take precedence over shorter patterns, i.e. a negative pattern must be of the same length or longer to reject matching patterns. Option -N cannot be specified with -P. This option may be repeated.-v, --invert-match Selected lines are those not matching any of the specified patterns.-w, --word-regexp The PATTERN is searched for as a word, such that the matching text is preceded by a non-word character and is followed by a non-word character. Word-like characters are Unicode letters, digits and connector punctuations such as underscore.-x, --line-regexp Select only those matches that exactly match the whole line, as if the patterns are surrounded by ^ and $.
See alsoBoolean query patterns with -%, -%%, --and, --not formore powerful Boolean query options than the traditional GNU/BSD grep options.
To display lines in filemyfile.sh
but not lines matching^[ \t]*#
:
ug -v '^[ \t]*#' myfile.sh
To searchmyfile.cpp
for lines withFIXME
andurgent
, but notScotty
:
ugrep FIXME myfile.cpp | ugrep urgent | ugrep -v Scotty
Same, but using-%
for Boolean queries:
ug -% 'FIXME urgent -Scotty' myfile.cpp
To search for decimals using pattern\d+
that do not start with0
usingnegative pattern0\d+
and excluding555
:
ug -e '\d+' -N '0\d+' -N 555 myfile.cpp
To search for words starting withdisp
without matchingdisplay
in filemyfile.py
by using a "negative pattern"-N '/<display\>'
where-N
specifies an additional negative pattern to skip matches:
ug -e '\<disp' -N '\<display\>' myfile.py
To search for lines with the worddisplay
in filemyfile.py
skipping thisword in strings and comments, where-f
specifies patterns in files which arepredefined patterns in this case:
ug -n -w 'display' -f python/zap_strings -f python/zap_comments myfile.py
To display lines that are not blank lines:
ug -x -e '.*' -N '\h*' myfile.py
Same, but using-v
and-x
with\h*
, i.e. pattern^\h*$
:
ug -v -x '\h*' myfile.py
To recursively list all Python files that do not contain the worddisplay
,allowing the word to occur in strings and comments:
ug -RL -tPython -w 'display' -f python/zap_strings -f python/zap_comments
--encoding=ENCODING The encoding format of the input. The default ENCODING is binary and UTF-8 which are the same. Note that option -U specifies binary PATTERN matching (text matching is the default.)
Binary, ASCII and UTF-8 files do not require this option to search them. AlsoUTF-16 and UTF-32 files do not require this option to search them, assumingthat UTF-16 and UTF-32 files start with a UTF BOM(byte order mark) as usual.Other file encodings require option--encoding=ENCODING
:
encoding | parameter |
---|---|
ASCII | n/a |
UTF-8 | n/a |
UTF-16 with BOM | n/a |
UTF-32 with BOM | n/a |
UTF-16 BE w/o BOM | UTF-16 orUTF-16BE |
UTF-16 LE w/o BOM | UTF-16LE |
UTF-32 w/o BOM | UTF-32 orUTF-32BE |
UTF-32 w/o BOM | UTF-32LE |
Latin-1 | LATIN1 orISO-8859-1 |
ISO-8859-1 | ISO-8859-1 |
ISO-8859-2 | ISO-8859-2 |
ISO-8859-3 | ISO-8859-3 |
ISO-8859-4 | ISO-8859-4 |
ISO-8859-5 | ISO-8859-5 |
ISO-8859-6 | ISO-8859-6 |
ISO-8859-7 | ISO-8859-7 |
ISO-8859-8 | ISO-8859-8 |
ISO-8859-9 | ISO-8859-9 |
ISO-8859-10 | ISO-8859-10 |
ISO-8859-11 | ISO-8859-11 |
ISO-8859-13 | ISO-8859-13 |
ISO-8859-14 | ISO-8859-14 |
ISO-8859-15 | ISO-8859-15 |
ISO-8859-16 | ISO-8859-16 |
MAC (CR=newline) | MAC |
MacRoman (CR=newline) | MACROMAN |
EBCDIC | EBCDIC |
DOS code page 437 | CP437 |
DOS code page 850 | CP850 |
DOS code page 858 | CP858 |
Windows code page 1250 | CP1250 |
Windows code page 1251 | CP1251 |
Windows code page 1252 | CP1252 |
Windows code page 1253 | CP1253 |
Windows code page 1254 | CP1254 |
Windows code page 1255 | CP1255 |
Windows code page 1256 | CP1256 |
Windows code page 1257 | CP1257 |
Windows code page 1258 | CP1258 |
KOI8-R | KOI8-R |
KOI8-U | KOI8-U |
KOI8-RU | KOI8-RU |
Note that regex patterns are always specified in UTF-8 (includes ASCII). Tosearch binary files with binary patterns, seesearching and displaying binary files with -U, -W, and -X.
To recursively list all files that are ASCII (i.e. 7-bit):
ug -L '[^[:ascii:]]'
To recursively list all files that are non-ASCII, i.e. UTF-8, UTF-16, andUTF-32 files with non-ASCII Unicode characters (U+0080 and up):
ug -l '[^[:ascii:]]'
To check if a file contains non-ASCII Unicode (U+0080 and up):
ug -q '[^[:ascii:]]' myfile && echo "contains Unicode"
To remove invalid Unicode characters from a file (note that-o
may not workbecause binary data is detected and rejected and newlines are added, but--format="%o%
does not check for binary and copies the match "as is"):
ug "[\p{Unicode}\n]" --format="%o" badfile.txt
To recursively list files with invalid UTF content (i.e. invalid UTF-8 bytesequences or files that contain any UTF-8/16/32 code points that are outsidethe valid Unicode range) by matching any code point with.
and by using anegative pattern-N '\p{Unicode}'
to ignore each valid Unicode character:
ug -l -e '.' -N '\p{Unicode}'
To display lines containing laughing face emojis:
ug '[😀-😏]' emojis.txt
The same results are obtained using\x{hhhh}
to select a Unicode characterrange:
ug '[\x{1F600}-\x{1F60F}]' emojis.txt
To display lines containing the names Gödel (or Goedel), Escher, or Bach:
ug 'G(ö|oe)del|Escher|Bach' GEB.txt wiki.txt
To search forlorem
in lower or upper case in a UTF-16 file that is markedwith a UTF-16 BOM:
ug -iw 'lorem' utf16lorem.txt
To search utf16lorem.txt when this file has no UTF-16 BOM, using--encoding
:
ug --encoding=UTF-16 -iw 'lorem' utf16lorem.txt
To search filespanish-iso.txt
encoded in ISO-8859-1:
ug --encoding=ISO-8859-1 -w 'año' spanish-iso.txt
-o, --only-matching Output only the matching part of lines. If -A, -B or -C is specified, fits the match and its context on a line within the specified number of columns.
Multiple lines may be matched by patterns that match newline characters. Useoption-o
to output the match only, not the full lines(s) that match.
To match a\n
line break, include\n
in the pattern to match the LFcharacter. If you want to match\r\n
and\n
line breaks, use\r?\n
orsimply use\R
to match any Unicode line break\r\n
,\r
,\v
,\f
,\n
,U+0085, U+2028 and U+2029.
To match C/C++/*...*/
multi-line comments:
ug '/\*(.*\n)*?.*\*+\/' myfile.cpp
To match C/C++ comments using the predefinedc/comments
patterns with-f c/comments
, restricted to the matching part only with option-o
:
ug -of c/comments myfile.cpp
Same assed -n '/begin/,/end/p'
: to match all lines between a line containingbegin
and the first line after that containingend
, using lazy repetition:
ug -o '.*begin(.|\n)*?end.*' myfile.txt
-A NUM, --after-context=NUM Output NUM lines of trailing context after matching lines. Places a --group-separator between contiguous groups of matches. If -o is specified, output the match with context to fit NUM columns after the match or shortens the match. See also options -B, -C and -y.-B NUM, --before-context=NUM Output NUM lines of leading context before matching lines. Places a --group-separator between contiguous groups of matches. If -o is specified, output the match with context to fit NUM columns before the match or shortens the match. See also options -A, -C and -y.-C NUM, --context=NUM Output NUM lines of leading and trailing context surrounding each matching line. Places a --group-separator between contiguous groups of matches. If -o is specified, output the match with context to fit NUM columns before and after the match or shortens the match. See also options -A, -B and -y.-y, --any-line Any line is output (passthru). Non-matching lines are output as context with a `-' separator. See also options -A, -B, and -C.--width[=NUM] Truncate the output to NUM visible characters per line. The width of the terminal window is used if NUM is not specified. Note that double wide characters in the output may result in wider lines.-o, --only-matching Output only the matching part of lines. If -A, -B or -C is specified, fits the match and its context on a line within the specified number of columns.
To display two lines of context before and after a matching line:
ug -C2 'FIXME' myfile.cpp
To show three lines of context after a matched line:
ug -A3 'FIXME.*' myfile.cpp:
To display one line of context before each matching line with a C functiondefinition (C names are non-Unicode):
ug -B1 -f c/functions myfile.c
To display one line of context before each matching line with a C++ functiondefinition (C++ names may be Unicode):
ug -B1 -f c++/functions myfile.cpp
To display any non-matching lines as context for matching lines with-y
:
ug -y -f c++/functions myfile.cpp
To display a hexdump of a matching line with one line of hexdump context:
ug -C1 -UX '\xaa\xbb\xcc' a.out
Context within a line is displayed with option-o
with a context option:
ug -o -C20 'pattern' myfile.cpp
Same, but with pretty output with headings, line numbers and column numbers(-k
) and showing context:
ug --pretty -oC20 'pattern' myfile.cpp
-f FILE, --file=FILE Read newline-separated patterns from FILE. White space in patterns is significant. Empty lines in FILE are ignored. If FILE does not exist, the GREP_PATH environment variable is used as path to FILE. If that fails, looks for FILE in /usr/local/share/ugrep/pattern. When FILE is a `-', standard input is read. This option may be repeated.--ignore-files[=FILE] Ignore files and directories matching the globs in each FILE that is encountered in recursive searches. The default FILE is `.gitignore'. Matching files and directories located in the directory of the FILE and in subdirectories below are ignored. Globbing syntax is the same as the --exclude-from=FILE gitignore syntax, but files and directories are excluded instead of only files. Directories are specifically excluded when the glob ends in a `/'. Files and directories explicitly specified as command line arguments are never ignored. This option may be repeated to specify additional files.-g GLOBS, --glob=GLOBS Search only files whose name matches the specified comma-separated list of GLOBS, same as --include='glob' for each `glob' in GLOBS. When a `glob' is preceded by a `!' or a `^', skip files whose name matches `glob', same as --exclude='glob'. When `glob' contains a `/', full pathnames are matched. Otherwise basenames are matched. When `glob' ends with a `/', directories are matched, same as --include-dir='glob' and --exclude-dir='glob'. A leading `/' matches the working directory. This option may be repeated and may be combined with options -M, -O and -t to expand searches. See `ugrep --help globs' and `man ugrep' section GLOBBING for details.-O EXTENSIONS, --file-extension=EXTENSIONS Search only files whose filename extensions match the specified comma-separated list of EXTENSIONS, same as --include='*.ext' for each `ext' in EXTENSIONS. When `ext' is preceded by a `!' or a `^', skip files whose filename extensions matches `ext', same as --exclude='*.ext'. This option may be repeated and may be combined with options -g, -M and -t to expand the recursive search.-t TYPES, --file-type=TYPES Search only files associated with TYPES, a comma-separated list of file types. Each file type corresponds to a set of filename extensions passed to option -O and filenames passed to option -g. For capitalized file types, the search is expanded to include files with matching file signature magic bytes, as if passed to option -M. When a type is preceded by a `!' or a `^', excludes files of the specified type. This option may be repeated.--stats Output statistics on the number of files and directories searched, and the inclusion and exclusion constraints applied.
The file types are listed withugrep -tlist
. The list is based onestablished filename extensions and "magic bytes". If you have a file typethat is not listed, use options-O
and/or-M
. You may want to define analias, e.g.alias ugft='ugrep -Oft'
as a shorthand to search files withfilename suffix.ft
.
To recursively display function definitions in C/C++ files (.h
,.hpp
,.c
,.cpp
etc.) with line numbers with-tc++
,-o
,-n
, and-f c++/functions
:
ug -on -tc++ -f c++/functions
To recursively display function definitions in.c
and.cpp
files with linenumbers with-Oc,cpp
,-o
,-n
, and-f c++/functions
:
ug -on -Oc,cpp -f c++/functions
To recursively list all shell files with-tShell
to match filename extensionsand files with shell shebangs, except files with suffix.sh
:
ug -l -tShell -O^sh ''
To recursively list all non-shell files with-t^Shell
:
ug -l -t^Shell ''
To recursively list all shell files with shell shebangs that have no shellfilename extensions:
ug -l -tShell -t^shell ''
To search for lines withFIXME
in C/C++ comments, excludingFIXME
inmulti-line strings:
ug -n 'FIXME' -f c++/zap_strings myfile.cpp
To read patternsTODO
andFIXME
from standard input to match lines in theinput, while excluding matches in C++ strings:
ug -on -f - -f c++/zap_strings myfile.cpp <<ENDTODOFIXMEEND
To display XML element and attribute tags in an XML file, restricted to thematching part with-o
, excluding tags that are placed in (multi-line)comments:
ug -o -f xml/tags -f xml/zap_comments myfile.xml
-z, --decompress Search compressed files and archives. Archives (.cpio, .pax, .tar) and compressed archives (e.g. .zip, .7z, .taz, .tgz, .tpz, .tbz, .tbz2, .tb2, .tz2, .tlz, .txz, .tzst) are searched and matching pathnames of files in archives are output in braces. When used with option --zmax=NUM, searches the contents of compressed files and archives stored within archives up to NUM levels. If -g, -O, -M, or -t is specified, searches files stored in archives whose filenames match globs, match filename extensions, match file signature magic bytes, or match file types, respectively. Supported compression formats: gzip (.gz), compress (.Z), zip, 7z, bzip2 (requires suffix .bz, .bz2, .bzip2, .tbz, .tbz2, .tb2, .tz2), lzma and xz (requires suffix .lzma, .tlz, .xz, .txz), lz4 (requires suffix .lz4), zstd (requires suffix .zst, .zstd, .tzst), brotli (requires suffix .br), bzip3 (requires suffix .bz3).--zmax=NUM When used with option -z (--decompress), searches the contents of compressed files and archives stored within archives by up to NUM expansion stages. The default --zmax=1 only permits searching uncompressed files stored in cpio, pax, tar, zip and 7z archives; compressed files and archives are detected as binary files and are effectively ignored. Specify --zmax=2 to search compressed files and archives stored in cpio, pax, tar, zip and 7z archives. NUM may range from 1 to 99 for up to 99 decompression and de-archiving steps. Increasing NUM values gradually degrades performance.
Files compressed with gzip (.gz
), compress (.Z
), bzip2 (.bz
,.bz2
,.bzip2
), lzma (.lzma
), xz (.xz
), lz4 (.lz4
), zstd (.zst
,.zstd
),brotli (.br
) and bzip3 (.bz3
) are searched with option-z
when thecorresponding libraries are installed and compiled with ugrep. This optiondoes not require files to be compressed. Uncompressed files are searched also,although slower.
Other compression formats can be searched withugrepfilters.
Archives (cpio, jar, pax, tar, zip and 7z) are searched with option-z
.Regular files in an archive that match are output with the archive pathnamesenclosed in{
and}
braces. Supported tar formats are v7, ustar, gnu,oldgnu, and pax. Supported cpio formats are odc, newc, and crc. Not supportedis the obsolete non-portable old binary cpio format. Archive formats cpio,tar, and pax are automatically recognized with option-z
based on theircontent, independent of their filename suffix.
By default, uncompressed archives stored within zip archives are also searched:all cpio, pax, and tar files stored in zip and 7z archives are automaticallyrecognized and searched. However, by default, compressed files stored withinarchives are not recognized, e.g. zip files stored within tar files are notsearched but rather all compressed files and archives are searched as if theyare binary files without decompressing them.
Specify--zmax=NUM
to search archives that contain compressed files andarchives for up toNUM
levels deep. The value ofNUM
may range from 1 to99 for up to 99 decompression and de-archiving steps to expand up to 99 nestedarchives. Larger--zmax=NUM
values degrade performance. It is unlikely youwill ever need 99 as--zmax=2
suffices for most practical use cases, such assearching zip files stored in tar files.
When option-z
is used with options-g
,-O
,-M
, or-t
, archives andcompressed and uncompressed files that match the filename selection criteria(glob, extension, magic bytes, or file type) are searched only. For example,ugrep -r -z -tc++
searches C++ files such asmain.cpp
and zip and tararchives that contain C++ files such asmain.cpp
. Also included in thesearch are compressed C++ files such asmain.cpp.gz
andmain.cpp.xz
whenpresent. Also any cpio, pax, tar, zip and 7z archives when present aresearched for C++ files that they contain, such asmain.cpp
. Use option--stats
to see a list of the glob patterns applied to filter file pathnamesin the recursive search and when searching archive contents.
When option-z
is used with options-g
,-O
,-M
, or-t
to search cpio,jar, pax, tar, zip and 7z archives, archived files that match the filenameselection criteria are searched only.
The gzip, compress, and zip formats are automatically detected, which is usefulwhen reading gzip-compressed data from standard input, e.g. input redirectedfrom a pipe. Other compression formats require a filename suffix:.bz
,.bz2
, or.bzip2
for bzip2,.lzma
for lzma,.xz
for xz,.lz4
for lz4,.zst
or.zstd
for zstd,.br
for brotli and.bz3
for bzip3. Also thecompressed tar archive shorthands.taz
,.tgz
and.tpz
for gzip,.tbz
,.tbz2
,.tb2
, and.tz2
for bzip2,.tlz
for lzma,.txz
for xz, and.tzst
for zstd are recognized. To search these formats with ugrep fromstandard input, use option--label='stdin.bz2'
for bzip2,--label='stdin.lzma'
for lzma,--label='stdin.xz'
for xz,--label='stdin.lz4
for lz4 and--label='stdin.zst
for zstd and so on. Thenamestdin
is arbitrary and may be omitted:
format | filename suffix | tar/pax archive short suffix | suffix required? | ugrep from stdin | library |
---|---|---|---|---|---|
gzip | .gz | .taz ,.tgz ,.tpz | no | automatic | libz |
compress | .Z | .taZ ,.tZ | no | automatic | built-in |
zip | .zip ,.zipx ,.ZIP | no | automatic | libz | |
7zip | .7z | yes | --label=.7z | built-in | |
bzip2 | .bz ,.bz2 ,.bzip2 | .tb2 ,.tbz ,.tbz2 ,.tz2 | yes | --label=.bz2 | libbz2 |
lzma | .lzma | .tlz | yes | --label=.lzma | liblzma |
xz | .xz | .txz | yes | --label=.xz | liblzma |
lz4 | .lz4 | yes | --label=.lz4 | liblz4 | |
zstd | .zst ,.zstd | .tzst | yes | --label=.zst | libzstd |
brotli | .br | yes | --label=.br | libbrotlidec | |
bzip3 | .bz3 | yes | --label=.bz3 | libbzip3 |
The gzip, bzip2, xz, lz4 and zstd formats support concatenated compressedfiles. Concatenated compressed files are searched as one single file.
Supported zip compression methods are stored (0), deflate (8), bzip2 (12), lzma(14), xz (95) and zstd (93). The bzip2, lzma, xz and zstd methods requireugrep to be compiled with the corresponding compression libraries.
Searching encrypted zip archives is not supported (perhaps in future releases,depending on requests for enhancements).
Searching 7zip archives takes a lot more RAM and more time compared to othermethods. The 7zip LZMA SDK implementation does not support streaming,requiring a physical seekable 7z file. This means that 7z files cannot besearched when nested within archives. Best is to avoid 7zip. Support for 7zipcan be disabled with./build.sh --disable-7zip
to build ugrep.
Option-z
uses threads for task parallelism to speed up searching largerfiles by running the decompressor concurrently with a search of thedecompressed stream.
To list all non-empty files stored in apackage.zip
archive, including thecontents of all cpio, pax, tar, zip and 7z files that are stored in it:
ug --zmax=2 -z -l '' package.zip
Same, but only list the Python source code files, including scripts that invokePython, with option-tPython
(ugrep -tlist
for details):
ug --zmax=2 -z -l -tPython '' package.zip
To search Python applications distributed as a tar file with their dependenciesincludes as wheels (zip files with Python code), searching for the wordmy_class
inapp.tgz
:
ug --zmax=2 -z -tPython -w my_class app.tgz
To recursively search C++ files including compressed files for the wordmy_function
, while skipping C and C++ comments:
ug -z -r -tc++ -Fw my_function -f cpp/zap_comments
To search bzip2, lzma, xz, lz4 and zstd compressed data on standard input,option--label
may be used to specify the extension corresponding to thecompression format to force decompression when the bzip2 extension is notavailable to ugrep, for example:
cat myfile.bz2 | ugrep -z --label='stdin.bz2' 'xyz'
To search filemain.cpp
inproject.zip
forTODO
andFIXME
lines:
ug -z -g main.cpp -w -e 'TODO' -e 'FIXME' project.zip
To search tarballproject.tar.gz
for C++ files withTODO
andFIXME
lines:
ug -z -tc++ -w -e 'TODO' -e 'FIXME' project.tar.gz
To search files matching the glob*.txt
inproject.zip
for the wordlicense
in any case (note that the-g
glob argument must be quoted):
ug -z -g '*.txt' -w -i 'license' project.zip
To display and page through all C++ files in tarballproject.tgz
:
ug --pager -z -tc++ '' project.tgz
To list the files matching the gitignore-style glob/**/projects/project1.*
inprojects.tgz
, by selecting files containing in the archive the textDecember 12
:
ug -z -l -g '/**/projects/project1.*' -F 'December 12' projects.tgz
To view the META-INF/MANIFEST.MF data in a jar file with-Ojar
and-OMF
toselect the jar file and the MF file therein (-Ojar
is required, otherwise thejar file will be skipped though we could read it from standard input instead):
ug -z -h -OMF,jar '' my.jar
To extract C++ files that containFIXME
fromproject.tgz
, we use-m1
with--format="'%z '"
to generate a space-separated list of pathnames of filelocated in the archive that match the wordFIXME
:
tar xzf project.tgz `ugrep -z -l -tc++ --format='%z ' -w FIXME project.tgz`
To perform a depth-first search withfind
, then usecpio
andugrep
tosearch the files:
find . -depth -print | cpio -o | ugrep -z 'xyz'
--ignore-files[=FILE] Ignore files and directories matching the globs in each FILE that is encountered in recursive searches. The default FILE is `.gitignore'. Matching files and directories located in the directory of the FILE and in subdirectories below are ignored. Globbing syntax is the same as the --exclude-from=FILE gitignore syntax, but files and directories are excluded instead of only files. Directories are specifically excluded when the glob ends in a `/'. Files and directories explicitly specified as command line arguments are never ignored. This option may be repeated to specify additional files.-M MAGIC, --file-magic=MAGIC Only files matching the signature pattern MAGIC are searched. The signature \"magic bytes\" at the start of a file are compared to the MAGIC regex pattern. When matching, the file will be searched. When MAGIC is preceded by a `!' or a `^', skip files with matching MAGIC signatures. This option may be repeated and may be combined with options -O and -t to expand the search. Every file on the search path is read, making searches potentially more expensive.-O EXTENSIONS, --file-extension=EXTENSIONS Search only files whose filename extensions match the specified comma-separated list of EXTENSIONS, same as --include='*.ext' for each `ext' in EXTENSIONS. When `ext' is preceded by a `!' or a `^', skip files whose filename extensions matches `ext', same as --exclude='*.ext'. This option may be repeated and may be combined with options -g, -M and -t to expand the recursive search.-t TYPES, --file-type=TYPES Search only files associated with TYPES, a comma-separated list of file types. Each file type corresponds to a set of filename extensions passed to option -O and filenames passed to option -g. For capitalized file types, the search is expanded to include files with matching file signature magic bytes, as if passed to option -M. When a type is preceded by a `!' or a `^', excludes files of the specified type. This option may be repeated.-g GLOBS, --glob=GLOBS Search only files whose name matches the specified comma-separated list of GLOBS, same as --include='glob' for each `glob' in GLOBS. When a `glob' is preceded by a `!' or a `^', skip files whose name matches `glob', same as --exclude='glob'. When `glob' contains a `/', full pathnames are matched. Otherwise basenames are matched. When `glob' ends with a `/', directories are matched, same as --include-dir='glob' and --exclude-dir='glob'. A leading `/' matches the working directory. This option may be repeated and may be combined with options -M, -O and -t to expand searches. See `ugrep --help globs' and `man ugrep' section GLOBBING for details.--stats Output statistics on the number of files and directories searched, and the inclusion and exclusion constraints applied.
To recursively list all files that start with#!
shebangs:
ug -l -M'#!' ''
To recursively list all files that start with#
but not with#!
shebangs:
ug -l -M'#' -M'^#!' ''
To recursively list all Python files (extension.py
or a shebang) with-tPython
:
ug -l -tPython ''
To recursively list all non-shell files with-t^Shell
:
ug -l -t^Shell ''
To recursively list Python files (extension.py
or a shebang) that haveimport statements, including hidden files with-.
:
ug -l. -tPython -f python/imports
-Z[best][+-~][MAX], --fuzzy=[best][+-~][MAX] Fuzzy mode: report approximate pattern matches within MAX errors. The default is -Z1: one deletion, insertion or substitution is allowed. If `+`, `-' and/or `~' is specified, then `+' allows insertions, `-' allows deletions and `~' allows substitutions. For example, -Z+~3 allows up to three insertions or substitutions, but no deletions. If `best' is specified, then only the best matching lines are output with the lowest cost per file. Option -Zbest requires two passes over a file and cannot be used with standard input or Boolean queries. Option --sort=best orders matching files by best match. The first character of an approximate match always matches a character at the beginning of the pattern. To fuzzy match the first character, replace it with a `.' or `.?'. Option -U applies fuzzy matching to ASCII and bytes instead of Unicode text. No whitespace may be given between -Z and its argument.
The beginning of a pattern always matches the first character of an approximatematch as a practical strategy to prevent many false "randomized" matches forshort patterns. This also greatly improves search speed. Make the firstcharacter optional to optionally match it, e.g.p?attern
or use a dot asthe start of the pattern to match any wide character (but this is slow).
Line feed (\n
) and NUL (\0
) characters are never deleted or substituted toensure that fuzzy matches do not extend the pattern match beyond the number oflines specified by the regex pattern.
Option-U
(--ascii
or--binary
) restricts fuzzy matches to ASCII andbinary only with edit distances measured in bytes. Otherwise, fuzzy patternmatching is performed with Unicode patterns and edit distances are measured inUnicode characters.
Option--sort=best
orders files by best match. Files with at least one exactmatch anywhere in the file are shown first, followed by files with approximatematches in increasing minimal edit distance order. That is, ordered by theminimum error (edit distance) found among all approximate matches per file.
To recursively search for approximate matches of the wordfoobar
with-Z
,i.e. approximate matching with one error, e.g.Foobar
,foo_bar
,foo bar
,fobar
and other forms with one missing, one extra or one deleted character:
ug -Z 'foobar'
Same, but matching words only with-w
and ignoring case with-i
:
ug -Z -wi 'foobar'
Same, but permit up to 2 insertions with-Z+2
, no deletions/substitutions(matches up to 2 extra characters, such asfoos bar
), insertions-only offersthe fastest fuzzy matching method:
ug -Z+3 -wi 'foobar'
Same, but sort matches from best (at least one exact match or fewest fuzzymatch errors) to worst:
ug -Z+3 -wi --sort=best 'foobar'
Note: because sorting by best match requires two passes over the inputfiles, the efficiency of concurrent searching is significantly reduced.
Same, but with customized formatting to show the edit distance "cost" of theapproximate matches with format field%Z
and%F
to show the pathname:
ug -Z+3 -wi --format='%F%Z:%O%~' --sort=best 'foobar'
Same, but this time count the matches with option-c
and display them with acustom format using%m
, where%Z
is theaverage cost per match:
ug -c -Z+3 -wi --format='%F%Z:%m%~' --sort=best 'foobar'
Note: options-c
and-l
do not report a meaningful%Z
value in the--format
output, because%Z
is the edit distance cost of a single match.
--hidden, -. Search hidden files and directories.
To recursively search the working directory, including hidden files anddirectories, for the wordlogin
in shell scripts:
ug -. -tShell 'login'
--filter=COMMANDS Filter files through the specified COMMANDS first before searching. COMMANDS is a comma-separated list of `exts:command [option ...]', where `exts' is a comma-separated list of filename extensions and `command' is a filter utility. Files matching one of `exts' are filtered. When `exts' is a `*', all files are filtered. One or more `option' separated by spacing may be specified, which are passed verbatim to the command. A `%' as `option' expands into the pathname to search. For example, --filter='pdf:pdftotext % -' searches PDF files. The `%' expands into a `-' when searching standard input. When a `%' is not specified, a filter utility should read from standard input and write to standard output. Option --label=.ext may be used to specify extension `ext' when searching standard input. This option may be repeated.--filter-magic-label=LABEL:MAGIC Associate LABEL with files whose signature "magic bytes" match the MAGIC regex pattern. Only files that have no filename extension are labeled, unless +LABEL is specified. When LABEL matches an extension specified in --filter=COMMANDS, the corresponding command is invoked. This option may be repeated.
The--filter
option associates one or more filter utilities with specificfilename extensions. A filter utility is selected based on the filenameextension and executed by forking a process: the utility's standard inputreads the open input file and the utility's standard output is searched. Whena%
is specified as an option to the utility, the%
is expanded to thepathname of the file to open and read by the utility.
When a specified utility is not found on the system, an error message isdisplayed. When a utility fails to produce output, e.g. when the specifiedoptions for the utility are invalid, the search is silently skipped.
Filtering does not apply to files stored in archives and compressed files. Afilter is usually applied to a file that is physically stored in the filesystem. Archived files are not physically stored.
Common filter utilities arecat
(concat, pass through),head
(select firstlines or bytes)tr
(translate),iconv
anduconv
(convert), and moreadvanced utilities, such as:
pdftotext
to convert pdf to textantiword
to convert doc to textpandoc
to convert .docx, .epub, and other documentformatsexiftool
to read meta informationembedded in image and video media formats.soffice
to convert office documentscsvkit
to convert spreadsheetsopenssl
toconvert certificates and key files to text and other formats
Theugrep+
andug+
commands use thepdftotext
,antiword
,pandoc
andexiftool
filters, when installed, to search pdfs, documents, e-books, andimage metadata.
Also decompressors may be used as filter utilities, such asunzip
,gunzip
,bunzip2
,unlzma
,unxz
,lzop
and7z
that decompress files to standardoutput when option--stdout
is specified. For example:
ug --filter='lzo:lzop -d --stdout -' ...ug --filter='gz:gunzip -d --stdout -' ...ug --filter='7z:7z x -so %' ...
The--filter='lzo:lzop -d --stdout -'
option decompresses files withextensionlzo
to standard output with--stdout
with the compressed streambeing read from standard input with-
. The--filter='7z:7z x -so -si
option decompresses files with extension7z
to standard output-so
whilereading standard input-si
with the compressed file contents.
Note thatugrep option-z
is typically faster to search compressed filescompared to--filter
.
The--filter
option may also be used to run a user-defined shell script tofilter files. For example, to invoke an action depending on the filenameextension of the%
argument. Another use case is to pass a file to more thanone filter, which can be accomplished with a shell script containing the linetool1 $1; tool2 $1
. This filters the file argument$1
withtool1
followed bytool2
to produce combined output to search for pattern matches.Likewise, we can use a script with the linetool1 $1 | tool2
to stack twofilterstool1
andtool2
.
The--filter
option may also be used as a predicate to skip certain filesfrom the search. As the most basic example, consider thefalse
utility thatexits with a nonzero exit code without reading input or producing output.Therefore,--filter='swp: false'
skips all.swp
files from recursivesearches. The same can be done more efficiently with-O^swp
. However,the--filter
option could invoke a script that determines if the filenamepassed as a%
argument meets certain constraints. If the constraint is metthe script copies standard input to standard output withcat
. If not, thescript exits.
Warning: option--filter
should not be used with utilities that modifyfiles. Otherwise searches may be unpredicatable. In the worst case files maybe lost, for example when the specified utility replaces or deletes the filepassed to the command with--filter
option%
.
To recursively search files including PDF files in the working directorywithout recursing into subdirectories (with-1
), for matches ofdrink me
using thepdftotext
filter to convert PDF to text without preserving pagebreaks:
ug -r -1 --filter='pdf:pdftotext -nopgbrk % -' 'drink me'
To recursively search text files foreat me
while converting non-printablecharacters in .txt and .md files using thecat -v
filter:
ug -r -ttext --filter='txt,md:cat -v' 'eat me'
The same, but specifying the .txt and .md filters separately:
ug -r -ttext --filter='txt:cat -v, md:cat -v' 'eat me'
To search the first 8K of a text file:
ug --filter='txt:head -c 8192' 'eat me' wonderland.txt
To recursively search and list the files that contain the wordAlice
,including .docx and .epub documents using thepandoc
filter:
ug -rl -w --filter='docx,epub:pandoc --wrap=preserve -t plain % -o -' 'Alice'
Important: thepandoc
utility requires an input file and will not readstandard input. Option%
expands into the full pathname of the file tosearch. The output format specified ismarkdown
, which is close enough totext to be searched.
To recursively search and list the files that contain the wordAlice
,including .odt, .doc, .docx, .rtf, .xls, .xlsx, .ppt, .pptx documents using thesoffice
filter:
ug -rl -w --filter='odt,doc,docx,rtf,xls,xlsx,ppt,pptx:soffice --headless --cat %' 'Alice'
Important: thesoffice
utility will not output any text when one or moreLibreOffice GUIs are open. Make sure to quit all LibreOffice apps first. Thislooks like a bug, but the LibreOffice developers do not appear to fix thisany time soon (unless perhaps more people complain?). You can work around thisproblem by specifying a specific user profile forsoffice
with the followingsemi-documented argument passed tosoffice
:-env:UserInstallation=file:///home/user/.libreoffice-alt
.
To recursively search and display rows of .csv, .xls, and .xlsx spreadsheetsthat contain10/6
using thein2csv
filter of csvkit:
ug -r -Ocsv,xls,xlsx --filter='xls,xlsx:in2csv %' '10/6'
To search .docx, .xlsx, and .pptx files converted to XML for a match with10/6
usingunzip
as a filter:
ug -lr -Odocx,xlsx,pptx --filter='docx,xlsx,pptx:unzip -p %' '10/6'
Important: unzipping docx, xlxs, pptx files produces extensive XML outputcontaining meta information and binary data such as images. By contrast,ugrep option-z
with-Oxml
selects the XML components only:
ug -z -lr -Odocx,xlsx,pptx,xml '10/6'
Note: docx, xlsx, and pptx are zip files containing multiple components.When selecting the XML components with option-Oxml
in docx, xlsx, and pptxdocuments, we should also specify-Odocx,xlsx,pptx
to search these type offiles, otherwise these files will be ignored.
To recurssively search X509 certificate files for lines withNot After
(e.g.to find expired certificates), usingopenssl
as a filter:
ug -r 'Not After' -Ocer,der,pem --filter='pem:openssl x509 -text,cer,crt,der:openssl x509 -text -inform der'
Note thatopenssl
warning messages are displayed on standard error. Ifa file cannot be converted it is probably in a different format. This canbe resolved by writing a shell script that executesopenssl
with optionsbased on the file content. Then write a script withugrep --filter
.
To search PNG files by filename extension with-tpng
usingexiftool
:
ug -r -i 'copyright' -tpng --filter='*:exiftool %'
Same, but also include files matching PNG "magic bytes" with-tPng
and--filter-magic-label='+png:\x89png\x0d\x0a\x1a\x0a'
to select thepng
filter:
ug -r -i 'copyright' -tPng --filter='png:exiftool %' --filter-magic-label='+png:\x89png\x0d\x0a\x1a\x0a'
Note that+png
overrides any filename extension match for--filter
.Otherwise, without a+
, the filename extension, when present, takes priorityover labelled magic patterns to invoke the corresponding filter command.TheLABEL
used with--filter-magic-label
and--filter
has no specificmeaning; any name or string that does not contain a:
or,
may be used.
--hexdump[=[1-8][a][bch][A[NUM]][B[NUM]][C[NUM]]] Output matches in 1 to 8 columns of 8 hexadecimal octets. The default is 2 columns or 16 octets per line. Argument `a' outputs a `*' for all hex lines that are identical to the previous hex line, `b' removes all space breaks, `c' removes the character column, `h' removes hex spacing, `A' includes up to NUM hex lines after a match, `B' includes up to NUM hex lines before a match and `C' includes up to NUM hex lines before and after a match. Arguments `A', `B' and `C' are the same as options -A, -B and -C when used with --hexdump. See also options -U, -W and -X.-U, --ascii, --binary Disables Unicode matching for binary file matching, forcing PATTERN to match bytes, not Unicode characters. For example, -U '\xa3' matches byte A3 (hex) instead of the Unicode code point U+00A3 represented by the UTF-8 sequence C2 A3. See also --dotall.-W, --with-hex Output binary matches in hexadecimal, leaving text matches alone. This option is equivalent to the --binary-files=with-hex option. To omit the matching line from the hex output, use both options -W and --hexdump. See also options -U.-X, --hex Output matches and matching lines in hexadecimal. This option is equivalent to the --binary-files=hex option. To omit the matching line from the hex output use option --hexdump. See also option -U.--dotall Dot `.' in regular expressions matches anything, including newline. Note that `.*' matches all input and should not be used.
Note that--hexdump
differs from-X
by omitting the matching line from thehex output, showing only the matching pattern using a minimal number of hexlines. Additional match context hex lines are output with the-ABC
contextoptions or with--hexdump=C3
to output 3 hex lines as context, for example.
To search a file for ASCII words, displaying text lines as usual while binarycontent is shown in hex with-U
and-W
:
ug -UW '\w+' myfile
To hexdump an entire file as a match with-X
:
ug -X '' myfile
To hexdump an entire file with-X
, displaying line numbers and byte offsetswith-nb
(here with-y
to display all line numbers):
ug -Xynb '' myfile
To hexdump lines containing one or more \0 in a (binary) file using anon-Unicode pattern with-U
and-X
:
ug -UX '\x00+' myfile
Same, but hexdump the entire file as context with-y
(note that thisline-based option does not permit matching patterns with newlines):
ug -UX -y '\x00+' myfile
Same, compacted to 32 bytes per line without the character column:
ug -UX -y '\x00+' myfile
To match the binary patternA3..A3.
(hex) in a binary file withoutUnicode pattern matching (which would otherwise match\xaf
as a Unicodecharacter U+00A3 with UTF-8 byte sequence C2 A3) and display the resultsin compact hex with--hexdump
with pager:
ug --pager --hexdump -U '\xa3[\x00-\xff]{2}\xa3[\x00-\xff]' a.out
Same, but using option--dotall
to let.
match any byte, includingnewline that is not matched by dot (the default as required by grep):
ug --dotall --pager --hexdump -U '\xa3.{2}\xa3.' a.out
To list all files containing a RPM signature, located in therpm
directory andrecursively below (see for examplelist of file signatures):
ug -RlU '\A\xed\xab\xee\xdb' rpm
-I Ignore matches in binary files. This option is equivalent to the --binary-files=without-match option.
To recursively search without following symlinks and ignoring binary files:
ug -rl -I 'xyz'
To ignore specific binary files with extensions such as .exe, .bin, .out, .a,use--exclude
or--exclude-from
:
ug -rl --exclude-from=ignore_binaries 'xyz'
whereignore_binaries
is a file containing a glob on each line to ignorematching files, e.g.*.exe
,*.bin
,*.out
,*.a
. Because the command isquite long to type, an alias for this is recommended, for exampleugs
(ugrepsource):
alias ugs="ugrep --exclude-from=~/ignore_binaries"ugs -rl 'xyz'
--ignore-files[=FILE] Ignore files and directories matching the globs in each FILE that is encountered in recursive searches. The default FILE is `.gitignore'. Matching files and directories located in the directory of the FILE and in subdirectories below are ignored. Globbing syntax is the same as the --exclude-from=FILE gitignore syntax, but files and directories are excluded instead of only files. Directories are specifically excluded when the glob ends in a `/'. Files and directories explicitly specified as command line arguments are never ignored. This option may be repeated to specify additional files.
Option--ignore-files
looks for.gitignore
, or the specifiedFILE
, inrecursive searches. When.gitignore
, or the specifiedFILE
, is found whiletraversing directory tree branches down, the.gitignore
file is used totemporarily extend the previous exclusions with the additional globs in.gitignore
to apply the combined exclusions to the directory tree rooted atthe.gitignore
location. Use--stats
to show the selection criteriaapplied to the search results and the locations of eachFILE
found. To avoidconfusion, files and directories specified as command-line arguments tougrep are never ignored.
Note that exclude glob patterns take priority over include glob patterns whenspecified with command line options. By contrast, negated glob patternsspecified with!
in--ignore-files
files take priority. This effectivelyoverrides the exclusions and resolves conflicts in favor of listing matchingfiles that are explicitly specified as exceptions and should be included in thesearch.
See alsoUsing gitignore-style globs to select directories and files to search.
To recursively search without following symlinks, while ignoring files anddirectories ignored by .gitignore (when present), use option--ignore-files
.Note that-r
is the default when no FILE arguments are specified, we use ithere to make the examples easier to follow.
ug -rl --ignore-files 'xyz'
Same, but includes hidden files with-.
rather than ignoring them:
ug -rl. --ignore-files 'xyz'
To recursively list all files that are not ignored by .gitignore (when present)with--ignore-files
:
ug -rl --ignore-files ''
Same, but list shell scripts that are not ignored by .gitignore, when present:
ug -rl -tShell '' --ignore-files
To recursively list all files that are not ignored by .gitignore and are alsonot excluded by.git/info/exclude
:
ug -rl '' --ignore-files --exclude-from=.git/info/exclude
Same, but by creating a symlink to.git/info/exclude
to make the exclusionsimplicit:
ln -s .git/info/exclude .ignoreug -rl '' --ignore-files --ignore-files=.ignore
-g GLOBS, --glob=GLOBS Search only files whose name matches the specified comma-separated list of GLOBS, same as --include='glob' for each `glob' in GLOBS. When a `glob' is preceded by a `!' or a `^', skip files whose name matches `glob', same as --exclude='glob'. When `glob' contains a `/', full pathnames are matched. Otherwise basenames are matched. When `glob' ends with a `/', directories are matched, same as --include-dir='glob' and --exclude-dir='glob'. A leading `/' matches the working directory. This option may be repeated and may be combined with options -M, -O and -t to expand searches. See `ugrep --help globs' and `man ugrep' section GLOBBING for details.--exclude=GLOB Skip files whose name matches GLOB using wildcard matching, same as -g ^GLOB. GLOB can use **, *, ?, and [...] as wildcards, and \\ to quote a wildcard or backslash character literally. When GLOB contains a `/', full pathnames are matched. Otherwise basenames are matched. When GLOB ends with a `/', directories are excluded as if --exclude-dir is specified. Otherwise files are excluded. Note that --exclude patterns take priority over --include patterns. GLOB should be quoted to prevent shell globbing. This option may be repeated.--exclude-dir=GLOB Exclude directories whose name matches GLOB from recursive searches, same as -g ^GLOB/. GLOB can use **, *, ?, and [...] as wildcards, and \\ to quote a wildcard or backslash character literally. When GLOB contains a `/', full pathnames are matched. Otherwise basenames are matched. Note that --exclude-dir patterns take priority over --include-dir patterns. GLOB should be quoted to prevent shell globbing. This option may be repeated.--exclude-from=FILE Read the globs from FILE and skip files and directories whose name matches one or more globs. A glob can use **, *, ?, and [...] as wildcards, and \ to quote a wildcard or backslash character literally. When a glob contains a `/', full pathnames are matched. Otherwise basenames are matched. When a glob ends with a `/', directories are excluded as if --exclude-dir is specified. Otherwise files are excluded. A glob starting with a `!' overrides previously-specified exclusions by including matching files. Lines starting with a `#' and empty lines in FILE are ignored. When FILE is a `-', standard input is read. This option may be repeated.--ignore-files[=FILE] Ignore files and directories matching the globs in each FILE that is encountered in recursive searches. The default FILE is `.gitignore'. Matching files and directories located in the directory of the FILE and in subdirectories below are ignored. Globbing syntax is the same as the --exclude-from=FILE gitignore syntax, but files and directories are excluded instead of only files. Directories are specifically excluded when the glob ends in a `/'. Files and directories explicitly specified as command line arguments are never ignored. This option may be repeated to specify additional files.--include=GLOB Search only files whose name matches GLOB using wildcard matching, same as -g GLOB. GLOB can use **, *, ?, and [...] as wildcards, and \\ to quote a wildcard or backslash character literally. When GLOB contains a `/', full pathnames are matched. Otherwise basenames are matched. When GLOB ends with a `/', directories are included as if --include-dir is specified. Otherwise files are included. Note that --exclude patterns take priority over --include patterns. GLOB should be quoted to prevent shell globbing. This option may be repeated.--include-dir=GLOB Only directories whose name matches GLOB are included in recursive searches, same as -g GLOB/. GLOB can use **, *, ?, and [...] as wildcards, and \\ to quote a wildcard or backslash character literally. When GLOB contains a `/', full pathnames are matched. Otherwise basenames are matched. Note that --exclude-dir patterns take priority over --include-dir patterns. GLOB should be quoted to prevent shell globbing. This option may be repeated.--include-from=FILE Read the globs from FILE and search only files and directories whose name matches one or more globs. A glob can use **, *, ?, and [...] as wildcards, and \ to quote a wildcard or backslash character literally. When a glob contains a `/', full pathnames are matched. Otherwise basenames are matched. When a glob ends with a `/', directories are included as if --include-dir is specified. Otherwise files are included. A glob starting with a `!' overrides previously-specified inclusions by excluding matching files. Lines starting with a `#' and empty lines in FILE are ignored. When FILE is a `-', standard input is read. This option may be repeated.-O EXTENSIONS, --file-extension=EXTENSIONS Search only files whose filename extensions match the specified comma-separated list of EXTENSIONS, same as --include='*.ext' for each `ext' in EXTENSIONS. When `ext' is preceded by a `!' or a `^', skip files whose filename extensions matches `ext', same as --exclude='*.ext'. This option may be repeated and may be combined with options -g, -M and -t to expand the recursive search.--stats Output statistics on the number of files and directories searched, and the inclusion and exclusion constraints applied.
See alsoIncluding or excluding mounted file systems from searches.
Gitignore-style glob syntax and conventions:
pattern | matches |
---|---|
* | anything except/ |
? | any one character except/ |
[abc-e] | one charactera ,b ,c ,d ,e |
[^abc-e] | one character nota ,b ,c ,d ,e ,/ |
[!abc-e] | one character nota ,b ,c ,d ,e ,/ |
/ | when used at the start of a glob, matches working directory |
**/ | zero or more directories |
/** | when at the end of a glob, matches everything after the/ |
\? | a? or any other character specified after the backslash |
When a glob pattern contains a path separator/
, the full pathname ismatched. Otherwise the basename of a file or directory is matched in recursivesearches. For example,*.h
matchesfoo.h
andbar/foo.h
.bar/*.h
matchesbar/foo.h
but notfoo.h
and notbar/bar/foo.h
.
When a glob pattern begins with a/
, files and directories are matched at theworking directory, not recursively. For example, use a leading/
to force/*.h
to matchfoo.h
but notbar/foo.h
.
When a glob pattern ends with a/
, directories are matched instead of files,same as--include-dir
.
When a glob starts with a!
as specified with-g!GLOB
, or specified in aFILE
with--include-from=FILE
or--exclude-from=FILE
, it is negated.
To view a list of inclusions and exclusions that were applied to a search, useoption--stats
.
To list only readable files with names starting withfoo
in the workingdirectory, that containxyz
, without producing warning messages with-s
and-l
:
ug -sl 'xyz' foo*
The same, but using deep recursion with inclusion constraints (note that-g'/foo*
is the same as--include='/foo*'
and-g'/foo*/'
is the same as--include-dir='/foo*'
, i.e. immediate subdirectories matching/foo*
only):
ug -rl 'xyz' -g'/foo*' -g'/foo*/'
Note that-r
is the default, we use it here to make the examples easier tofollow.
To exclude directorybak
located in the working directory:
ug -rl 'xyz' -g'^/bak/'
To exclude all directoiesbak
at any directory level deep:
ug -rl 'xyz' -g'^bak/'
To only list files in the working directory and its subdirectorydoc
,that containxyz
(note that-g'/doc/'
is the same as--include-dir='/doc'
, i.e. immediate subdirectorydoc
only):
ug -rl 'xyz' -g'/doc/'
To only list files that are on a subdirectory pathdoc
that includessubdirectoryhtml
anywhere, that containxyz
:
ug -rl 'xyz' -g'doc/**/html/'
To only list files in the working directory and in the subdirectoriesdoc
anddoc/latest
but not below, that containxyz
:
ug -rl 'xyz' -g'/doc/' -g'/doc/latest/'
To recursively list .cpp files in the working directory and any subdirectoryat any depth, that containxyz
:
ug -rl 'xyz' -g'*.cpp'
The same, but using a .gitignore-style glob that matches pathnames (globs with/
) instead of matching basenames (globs without/
) in the recursive search:
ug -rl 'xyz' -g'**/*.cpp'
Same, but using option-Ocpp
to match file name extensions:
ug -rl -Ocpp 'xyz'
To recursively list all files in the working directory and below that are notignored by a specific .gitignore file:
ug -rl '' --exclude-from=.gitignore
To recursively list all files in the working directory and below that are notignored by one or more .gitignore files, when any are present:
ug -rl '' --ignore-files
--exclude-fs=MOUNTS Exclude file systems specified by MOUNTS from recursive searches. MOUNTS is a comma-separated list of mount points or pathnames to directories. When MOUNTS is not specified, only descends into the file systems associated with the specified file and directory search targets, i.e. excludes all other file systems. Note that --exclude-fs=MOUNTS take priority over --include-fs=MOUNTS. This option may be repeated.--include-fs=MOUNTS Only file systems specified by MOUNTS are included in recursive searches. MOUNTS is a comma-separated list of mount points or pathnames to directories. When MOUNTS is not specified, restricts recursive searches to the file system of the working directory, same as --include-fs=. (dot). Note that --exclude-fs=MOUNTS take priority over --include-fs=MOUNTS. This option may be repeated.
These options control recursive searches across file systems by comparingdevice numbers. Mounted devices and symbolic links to files and directorieslocated on mounted file systems may be included or excluded from recursivesearches by specifying a mount point or a pathname of any directory on the filesystem to specify the applicable file system.
Note that a list of mounted file systems is typically stored in/etc/mtab
.
To restrict recursive searches to the file system(s) of the search targetsonly, without crossing into other file systems (similar tofind
option-x
):
ug -rl --exclude-fs 'xyz' /sys /var
To restrict recursive searches to the file system of the working directoryonly, without crossing into other file systems:
ug -l --include-fs 'xyz'
In fact, for this case we can use--exclude-fs
because we search the workingdirectory as the target and we want to exclude all other file systems:
ug -l --exclude-fs 'xyz'
To exclude the file systems mounted at/dev
and/proc
from recursivesearches:
ug -l --exclude-fs=/dev,/proc 'xyz'
To only include the file system associated with drived:
in recursivesearches:
ug -l --include-fs=d:/ 'xyz'
To excludefuse
andtmpfs
type file systems from recursive searches:
exfs=`ugrep -w -e fuse -e tmpfs /etc/mtab | ugrep -P '^\S+ (\S+)' --format='%,%1'`ug -l --exclude-fs="$exfs" 'xyz'
-c, --count Only a count of selected lines is written to standard output. If -o or -u is specified, counts the number of patterns matched. If -v is specified, counts the number of non-matching lines. If -m1, (with a comma or --min-count=1) is specified, counts only matching files without outputting zero matches.
To count the number of lines in a file:
ug -c '' myfile.txt
To count the number of lines withTODO
:
ug -c -w 'TODO' myfile.cpp
To count the total number ofTODO
in a file, use-c
and-o
:
ug -co -w 'TODO' myfile.cpp
To count the number of ASCII words in a file:
ug -co '[[:word:]]+' myfile.txt
To count the number of ASCII and Unicode words in a file:
ug -co '\w+' myfile.txt
To count the number of Unicode characters in a file:
ug -co '\p{Unicode}' myfile.txt
To count the number of zero bytes in a file:
ug -UX -co '\x00' image.jpg
-b, --byte-offset The offset in bytes of a matched line is displayed in front of the respective matched line. When used with option -u, displays the offset in bytes of each pattern matched. Byte offsets are exact for ASCII, UTF-8, and raw binary input. Otherwise, the byte offset in the UTF-8 converted input is displayed.-H, --with-filename Always print the filename with output lines. This is the default when there is more than one file to search.-k, --column-number The column number of a matched pattern is displayed in front of the respective matched line, starting at column 1. Tabs are expanded when columns are counted, see option --tabs.-n, --line-number Each output line is preceded by its relative line number in the file, starting at line 1. The line number counter is reset for each file processed.-T, --initial-tab Add a tab space to separate the file name, line number, column number, and byte offset with the matched line.
To display the file name-H
, line-n
, and column-k
numbers of matches inmyfile.cpp
, with spaces and tabs to space the columns apart with-T
:
ug -THnk 'main' myfile.cpp
To display the line with-n
of wordmain
inmyfile.cpp
:
ug -nw 'main' myfile.cpp
To display the entire filemyfile.cpp
with line-n
numbers:
ug -n '' myfile.cpp
To recursively search for C++ files withmain
, showing the line and columnnumbers of matches with-n
and-k
:
ug -r -nk -tc++ 'main'
To display the byte offset of matches with-b
:
ug -r -b -tc++ 'main'
To display the line and column numbers of matches in XML with--xml
:
ug -r -nk --xml -tc++ 'main'
--color[=WHEN], --colour[=WHEN] Mark up the matching text with the expression stored in the GREP_COLOR or GREP_COLORS environment variable. The possible values of WHEN can be `never', `always', or `auto', where `auto' marks up matches only when output on a terminal. The default is `auto'.--colors=COLORS, --colours=COLORS Use COLORS to mark up text. COLORS is a colon-separated list of one or more parameters `sl=' (selected line), `cx=' (context line), `mt=' (matched text), `ms=' (match selected), `mc=' (match context), `fn=' (file name), `ln=' (line number), `cn=' (column number), `bn=' (byte offset), `se=' (separator), `qp=' (TUI prompt), `qe=' (TUI errors), `qr=' (TUI regex), `qm=' (TUI regex meta characters), `ql=' (TUI regex lists and literals), `qb=' (TUI regex braces). Parameter values are ANSI SGR color codes or `k' (black), `r' (red), `g' (green), `y' (yellow), `b' (blue), `m' (magenta), `c' (cyan), `w' (white), or leave empty for no color. Upper case specifies background colors. A `+' qualifies a color as bright. A foreground and a background color may be combined with font properties `n' (normal), `f' (faint), `h' (highlight), `i' (invert), `u' (underline). Parameter `hl' enables file name hyperlinks. Parameter `rv' reverses the `sl=' and `cx=' parameters when option -v is specified. Selectively overrides GREP_COLORS. Legacy grep single parameter codes may be specified, for example --colors='7;32' or --colors=ig to set ms (match selected).--tag[=TAG[,END]] Disables colors to mark up matches with TAG. END marks the end of a match if specified, otherwise TAG. The default is `___'.--pager[=COMMAND] When output is sent to the terminal, uses COMMAND to page through the output. COMMAND defaults to environment variable PAGER when defined or `less'. Enables --heading and --line-buffered.--pretty[=WHEN] When output is sent to a terminal, enables --color, --heading, -n, --sort, --tree and -T when not explicitly disabled. WHEN can be `never', `always', or `auto'. The default is `auto'.--tree, -^ Output directories with matching files in a tree-like format for option -c or --count, -l or --files-with-matches, -L or --files-without-match. This option is enabled by --pretty when the output is sent to a terminal.
To change the color palette, set theGREP_COLORS
environment variable or use--colors=COLORS
. The value is a colon-separated list of ANSI SGR parametersthat defaults tocx=33:mt=1;31:fn=1;35:ln=1;32:cn=1;32:bn=1;32:se=36
:
param | result |
---|---|
sl= | selected lines |
cx= | context lines |
rv | Swaps thesl= andcx= capabilities when-v is specified |
mt= | matching text in any matching line |
ms= | matching text in a selected line. The substring mt= by default |
mc= | matching text in a context line. The substring mt= by default |
fn= | file names |
ln= | line numbers |
cn= | column numbers |
bn= | byte offsets |
se= | separators |
hl | hyperlink file names, same as--hyperlink |
qp= | TUI prompt |
qe= | TUI errors |
qr= | TUI regex |
qm= | TUI regex meta characters |
ql= | TUI regex lists and literals |
qb= | TUI regex braces |
Multiple SGR codes may be specified for a single parameter when separated by asemicolon, e.g.mt=1;31
specifies bright red. The following SGR codes areavailable on most color terminals:
code | c | effect | code | c | effect |
---|---|---|---|---|---|
0 | n | normal font and color | 2 | f | faint (not widely supported) |
1 | h | highlighted bold font | 21 | H | highlighted bold off |
4 | u | underline | 24 | U | underline off |
7 | i | invert video | 27 | I | invert off |
30 | k | black text | 90 | +k | bright gray text |
31 | r | red text | 91 | +r | bright red text |
32 | g | green text | 92 | +g | bright green text |
33 | y | yellow text | 93 | +y | bright yellow text |
34 | b | blue text | 94 | +b | bright blue text |
35 | m | magenta text | 95 | +m | bright magenta text |
36 | c | cyan text | 96 | +c | bright cyan text |
37 | w | white text | 97 | +w | bright white text |
40 | K | black background | 100 | +K | bright gray background |
41 | R | dark red background | 101 | +R | bright red background |
42 | G | dark green background | 102 | +G | bright green background |
43 | Y | dark yellow backgrounda | 103 | +Y | bright yellow background |
44 | B | dark blue background | 104 | +B | bright blue background |
45 | M | dark magenta background | 105 | +M | bright magenta background |
46 | C | dark cyan background | 106 | +C | bright cyan background |
47 | W | dark white background | 107 | +W | bright white background |
See WikipediaANSI escape code - SGR parameters
For quick and easy color specification, the corresponding single-letter colornames may be used in place of numeric SGR codes and semicolons are not requiredto separate color names. Color names and numeric codes may be mixed.
For example, to display matches in underlined bright green on bright selectedlines, aiding in visualizing white space in matches and file names:
export GREP_COLORS='sl=1:cx=33:ms=1;4;32;100:mc=1;4;32:fn=1;32;100:ln=1;32:cn=1;32:bn=1;32:se=36'
The same, but with single-letter color names:
export GREP_COLORS='sl=h:cx=y:ms=hug+K:mc=hug:fn=hg+K:ln=hg:cn=hg:bn=hg:se=c'
Another color scheme that works well:
export GREP_COLORS='cx=hb:ms=hiy:mc=hic:fn=hi+y+K:ln=hg:cn=hg:bn=hg:se='
Modern Windows command interpreters support ANSI escape codes. Named ornumeric colors can be set withSET GREP_COLORS
, for example:
SET GREP_COLORS=sl=1;37:cx=33:mt=1;31:fn=1;35:ln=1;32:cn=1;32:bn=1;32:se=36
To disable colors on Windows:
SET GREP_COLORS=""
Color intensities may differ per platform and per terminal program used, whichaffects readability.
Option-y
outputs every line of input, including non-matching lines ascontext. The use of color helps distinguish matches from non-matching context.
To copy silver searcher's color palette:
export GREP_COLORS='mt=30;43:fn=1;32:ln=1;33:cn=1;33:bn=1;33'
To produce color-highlighted results (--color
is redundance since it is thedefault):
ug --color -r -n -k -tc++ 'FIXME.*'
To page through the results with pager (less -R
by default):
ug --pager -r -n -k -tc++ 'FIXME'
To display a hexdump of a zip file itself (i.e. without decompressing), withcolor-highlighted matches of the zip magic bytesPK\x03\x04
(--color
isredundant since it is the default):
ug --color -y -UX 'PK\x03\x04' some.zip
To use predefined patterns to list all#include
and#define
in C++ files:
ug --pretty -r -n -tc++ -f c++/includes -f c++/defines
Same, but overriding the color of matches as inverted yellow (reverse video)and headings with yellow on blue using--pretty
:
ug --pretty --colors="ms=yi:fn=hyB" -r -n -tc++ -f c++/includes -f c++/defines
To list all#define FOO...
macros in C++ files, color-highlighted:
ug --color=always -r -n -tc++ -f c++/defines | ug 'FOO.*'
Same, but restricted to.cpp
files only:
ug --color=always -r -n -Ocpp -f c++/defines | ug 'FOO.*'
To search tarballs for matching names of PDF files (assuming bash is our shell):
for tb in *.tar *.tar.gz *.tgz; do echo "$tb"; tar tfz "$tb" | ugrep '.*\.pdf$'; done
--cpp Output file matches in C++. See also options --format and -u.--csv Output file matches in CSV. If -H, -n, -k, or -b is specified, additional values are output. See also options --format and -u.--json Output file matches in JSON. If -H, -n, -k, or -b is specified, additional values are output. See also options --format and -u.--xml Output file matches in XML. If -H, -n, -k, or -b is specified, additional values are output. See also options --format and -u.
To recursively search for lines withTODO
and display C++ file matches inJSON with line number properties:
ug -tc++ -n --json 'TODO'
To recursively search for lines withTODO
and display C++ file matches inXML with line and column number attributes:
ug -tc++ -nk --xml 'TODO'
To recursively search for lines withTODO
and display C++ file matches in CSVformat with file pathname, line number, and column number fields:
ug -tc++ --csv -Hnk 'TODO'
To extract a table from an HTML file and put it in C/C++ source code using-o
:
ug -o --cpp '<tr>.*</tr>' index.html > table.cpp
--format=FORMAT Output FORMAT-formatted matches. For example --format='%f:%n:%O%~' outputs matching lines `%O' with filename `%f` and line number `%n' followed by a newline `%~'. If -P is specified, FORMAT may include `%1' to `%9', `%[NUM]#' and `%[NAME]#' to output group captures. A `%%' outputs `%'. See `ugrep --help format' and `man ugrep' section FORMAT for details. When option -o is specified, option -u is also enabled. Context options -A, -B, -C and -y are ignored.-P, --perl-regexp Interpret PATTERN as a Perl regular expression.
Use option-P
to use group captures and backreferences. Capturing groups inregex patterns are parenthesized expressions(pattern)
. The first group isreferenced inFORMAT
by%1
, the second by%2
and so on. Named capturesare of the form(?<NAME>pattern)
and are referenced inFORMAT
by%[NAME]#
.
The following output formatting options may be used. TheFORMAT
string%
-fields are listed in a table further below:
option | result |
---|---|
--format-begin=FORMAT | FORMAT beginning the search |
--format-open=FORMAT | FORMAT opening a file and a match was found |
--format=FORMAT | FORMAT for each match in a file |
--format-close=FORMAT | FORMAT closing a file and a match was found |
--format-end=FORMAT | FORMAT ending the search |
The following tables show the formatting options corresponding to--csv
,--json
, and--xml
.
option | format string (within quotes) |
---|---|
--format-open | '%+' |
--format | '%[,]$%H%N%K%B%V%~%u' |
option | format string (within quotes) |
---|---|
--format-begin | '[' |
--format-open | '%,%~ {%~ %[,%~ ]$%["file": ]H"matches": [' |
--format | '%,%~ { %[, ]$%["line": ]N%["column": ]K%["offset": ]B"match": %J }%u' |
--format-close | '%~ ]%~ }' |
--format-end | '%~]%~' |
option | format string (within quotes) |
---|---|
--format-begin | '<grep>%~' |
--format-open | ' <file%["]$%[ name="]I>%~' |
--format | ' <match%["]$%[ line="]N%[ column="]K%[ offset="]B>%X</match>%~%u' |
--format-close | ' </file>%~' |
--format-end | '</grep>%~' |
option | format string (within quotes) |
---|---|
--format-open | '%+' |
--format | '%F%n%s%K%B%~%u' |
The following fields may be used in theFORMAT
string:
field | output |
---|---|
%% | the percentage sign |
%~ | a newline (LF or CRLF in Windows) |
%F | if option-H is used: the file pathname and separator |
%[TEXT]F | if option-H is used:TEXT , the file pathname and separator |
%f | the file pathname |
%a | the file basename without directory path |
%p | the directory path to the file |
%z | the pathname in a (compressed) archive, without{ and} |
%H | if option-H is used: the quoted pathname and separator,\" and\\ replace" and\ |
%+ | if option-+ or--heading is used:%F and a newline character, suppress all%F and%H afterward |
%[TEXT]H | if option-H is used:TEXT , the quoted pathname and separator,\" and\\ replace" and\ |
%h | the quoted file pathname,\" and\\ replace" and\ |
%I | if option-H is used: the pathname in XML and separator |
%[TEXT]I | if option-H is used:TEXT , the pathname as XML and separator |
%i | the file pathnames as XML |
%N | if option-n is used: the line number and separator |
%[TEXT]N | if option-n is used:TEXT , the line number and separator |
%n | the line number of the match |
%l | the last line number of the match (multi-line matching) |
%L | the number of lines matched (multi-line matching) |
%K | if option-k is used: the column number and separator |
%[TEXT]K | if option-k is used:TEXT , the column number and separator |
%k | the column number of the match |
%A | byte range (offset and end) of a match in hex |
%B | if option-b is used: the byte offset and separator |
%[TEXT]B | if option-b is used:TEXT , the byte offset and separator |
%b | the byte offset of the match |
%T | if option-T is used:TEXT and a tab character |
%[TEXT]T | if option-T is used:TEXT and a tab character |
%t | a tab character |
%[SEP]$ | set field separator toSEP for the rest of the format fields |
%[TEXT]< | if the first match:TEXT |
%[TEXT]> | if not the first match:TEXT |
%, | if not the first match: a comma, same as%[,]> |
%: | if not the first match: a colon, same as%[:]> |
%; | if not the first match: a semicolon, same as%[;]> |
%│ | if not the first match: a vertical bar, same as%[│]> |
%S | if not the first match: separator, see also%[SEP]$ |
%[TEXT]S | if not the first match:TEXT and separator, see also%[SEP]$ |
%s | the separator, see also%[TEXT]S and%[SEP]$ |
%R | if option--break or--heading is used: a newline |
%m | the number of matches, sequential (or number of matching files with--format-end ) |
%M | the number of matching lines (or number of matching files with--format-end ) |
%O | the matching line is output as is (a raw string of bytes) |
%o | the match is output as is (a raw string of bytes) |
%Q | the matching line as a quoted string,\" and\\ replace" and\ |
%q | the match as a quoted string,\" and\\ replace" and\ |
%C | the matching line formatted as a quoted C/C++ string |
%c | the match formatted as a quoted C/C++ string |
%J | the matching line formatted as a quoted JSON string |
%j | the match formatted as a quoted JSON string |
%V | the matching line formatted as a quoted CSV string |
%v | the match formatted as a quoted CSV string |
%X | the matching line formatted as XML character data |
%x | the match formatted as XML character data |
%Y | the matching line formatted in hex |
%y | the match formatted in hex |
%A | byte range of the match in hex |
%w | the width of the match, counting (wide) characters |
%d | the size of the match, counting bytes |
%e | the ending byte offset of the match |
%Z | the edit distance cost of an approximate match with option-Z |
%u | select unique lines only unless option -u is used |
%[hhhh]U | U+hhhh Unicode code point |
%[CODE]= | a color CODE, such asms , seecolors |
%= | turn color off |
%1 %2 ...%9 | the first regex group capture of the match, and so on up to group%9 , requires option-P |
%[NUM]# | the group captureNUM ; requires option-P |
%[NUM]b | the byte offset of the group captureNUM ; requires option-P |
%[NUM]e | the ending byte offset of the group captureNUM ; requires option-P |
%[NUM]d | the byte length of the group captureNUM ; requires option-P |
%[NUM]j | the group captureNUM as JSON; requires option-P |
%[NUM]q | the group captureNUM quoted; requires option-P |
%[NUM]x | the group captureNUM as XML; requires option-P |
%[NUM]y | the group captureNUM as hex; requires option-P |
%[NUM]v | the group captureNUM as CSV; requires option-P |
%[NUM1|NUM2|...]# | the first group captureNUM that matched; requires option-P |
%[NUM1|NUM2|...]b | the byte offset of the first group captureNUM that matched; requires option-P . |
%[NUM1|NUM2|...]e | the ending byte offset of the first group captureNUM that matched; requires option-P . |
%[NUM1|NUM2|...]d | the byte length of the first group captureNUM that matched; requires option-P . |
%[NUM1|NUM2|...]j | the first group captureNUM that matched, as JSON; requires option-P |
%[NUM1|NUM2|...]q | the first group captureNUM that matched, quoted; requires option-P |
%[NUM1|NUM2|...]x | the first group captureNUM that matched, as XML; requires option-P |
%[NUM1|NUM2|...]y | the first group captureNUM that matched, as hex; requires option-P |
%[NUM1|NUM2|...]v | the first group captureNUM that matched, as CSV; requires option-P |
%[NAME]# | theNAME d group capture; requires option-P and capturing pattern(?<NAME>PATTERN) |
%[NAME]b | the byte offset of theNAME d group capture; requires option-P and capturing pattern(?<NAME>PATTERN) . |
%[NAME]e | the ending byte offset of theNAME d group capture; requires option-P and capturing pattern(?<NAME>PATTERN) . |
%[NAME]d | the byte length of theNAME d group capture; requires option-P and capturing pattern(?<NAME>PATTERN) . |
%[NAME]j | theNAME d group capture as JSON; requires option-P and capturing pattern(?<NAME>PATTERN) |
%[NAME]q | theNAME d group capture quoted; requires option-P and capturing pattern(?<NAME>PATTERN) |
%[NAME]x | theNAME d group capture as XML; requires option-P and capturing pattern(?<NAME>PATTERN) |
%[NAME]y | theNAME d group capture as hex; requires option-P and capturing pattern(?<NAME>PATTERN) |
%[NAME]v | theNAME d group capture as CSV; requires option-P and capturing pattern(?<NAME>PATTERN) |
%[NAME1|NAME2|...]# | the firstNAME d group capture that matched; requires option-P and capturing pattern(?<NAME>PATTERN) |
%[NAME1|NAME2|...]b | the byte offset of the firstNAME d group capture that matched; requires option-P and capturing pattern(?<NAME>PATTERN) |
%[NAME1|NAME2|...]e | the ending byte offset of the firstNAME d group capture that matched; requires option-P and capturing pattern(?<NAME>PATTERN) |
%[NAME1|NAME2|...]d | the byte length of the firstNAME d group capture that matched; requires option-P and capturing pattern(?<NAME>PATTERN) |
%[NAME1|NAME2|...]j | the firstNAME d group capture that matched, as JSON; requires option-P and capturing pattern(?<NAME>PATTERN) |
%[NAME1|NAME2|...]q | the firstNAME d group capture that matched, quoted; requires option-P and capturing pattern(?<NAME>PATTERN) |
%[NAME1|NAME2|...]x | the firstNAME d group capture that matched, as XML; requires option-P and capturing pattern(?<NAME>PATTERN) |
%[NAME1|NAME2|...]y | the firstNAME d group capture that matched, as hex; requires option-P and capturing pattern(?<NAME>PATTERN) |
%[NAME1|NAME2|...]v | the firstNAME d group capture that matched, as CSV; requires option-P and capturing pattern(?<NAME>PATTERN) |
%G | list of group capture indices/names of the match (see note) |
%[TEXT1|TEXT2|...]G | list of TEXT indexed by group capture indices that matched; requires option-P |
%g | the group capture index of the match or 1 (see note) |
%[TEXT1|TEXT2|...]g | the first TEXT indexed by the first group capture index that matched; requires option-P |
Note:
- Formatted output is written without a terminating newline, unless
%~
isexplicitly specified in the format string. - Option
-o
changes the output of the%O
and%Q
fields to output thematch only. - Options
-c
,-l
and-o
change the output of%C
,%J
,%X
and%Y
accordingly - The
[TEXT]
part of a field is optional and may be omitted. When present,the argument must be placed in[]
brackets, for example%[,]F
to output acomma, the pathname, and a separator, when option-H
is used. - Numeric fields such as
%n
are padded with spaces when%{width}n
isspecified. - Matching line fields such as
%O
are cut to width when%{width}O
isspecified or when%{-width}O
is specified to cut from the end of the line. - Character context on a matching line before or after a match is output when
%{-width}o
or%{+width}o
is specified for match fields such as%o
,where%{width}o
without a +/- sign cuts the match to the specified width. - Fields
%[SEP]$
and%u
are switches and do not write anything to theoutput. - The separator used by
%F
,%H
,%N
,%K
,%B
,%S
, and%G
may bechanged by preceding the field with a%[SEP]$
. When[SEP]
is notprovided, reverts the separator to the default separator or the separatorspecified by--separator
. - Formatted output is written for each matching pattern, which means that aline may be output multiple times when patterns match more than once on thesame line. When field
%u
is found anywhere in the specified format string,matching lines are output only once unless option-u
,--ungroup
is used or when a newline is matched. - The group capture index value output by
%g
corresponds to the index of thesub-pattern matched among the alternations in the pattern when option-P
isnot used. For examplefoo|bar
matchesfoo
with index 1 andbar
withindex 2. With option-P
, the index corresponds to the number of the firstgroup captured in the specified pattern. - The strings specified in the list
%[TEXT1|TEXT2|...]G
and%[TEXT1|TEXT2|...]g
should correspond to the group capture index (see thenote above), i.e.TEXT1
is output for index 1,TEXT2
is output for index2, and so on. If the list is too short, the index value is output or thename of a named group capture is output. - Option
-T
and--pretty
add right-justifying spacing to fields%N
and%K
if no leading[TEXT]
part is specified. - Field
%+
may be used in--format-open
to output the pathname heading anda newline break, respectively. Field%+
suppresses%a
,%F
,%f
,%H
,%h
and%p
output.
To output matching lines faster by omitting the header output and binary matchchecks, using--format
with field%O
(output matching line as is) and field%~
(output newline):
ug --format='%O%~' 'href=' index.html
Same, but also displaying the line and column numbers:
ug --format='%n%k: %O%~' 'href=' index.html
Same, but display a line at most once when matching multiple patterns, unlessoption-u
is used:
ug --format='%u%n%k: %O%~' 'href=' index.html
To string together a list of unique line numbers of matches, separated bycommas with field%,
:
ug --format='%u%,%n' 'href=' index.html
To output the matching part of a line only with field%o
(or option-o
withfield%O
):
ug --format='%o%~' "href=[\"'][^\"'][\"']" index.html
To string together the pattern matches as CSV-formatted strings with field%v
separated by commas with field%,
:
ug --format='%,%v' "href=[\"'][^\"'][\"']" index.html
To output matches in CSV (comma-separated values), the same as option--csv
(works with options-H
,-n
,-k
,-b
to add CSV values):
ug --format='"%[,]$%H%N%K%B%V%~%u"' 'href=' index.html
To output matches in AckMate format:
ug --format=":%f%~%n;%k %w:%O%~" 'href=' index.html
To output the sub-pattern indices 1, 2, and 3 on the left to the match for thethree patternsfoo
,bar
, andbaz
in filefoobar.txt
:
ug --format='%g: %o%~' 'foo|bar|baz' foobar.txt
Same, but using a filefoos
containing three lines withfoo
,bar
, andbaz
, where option-F
is used to match strings instead of regex:
ug -F -f foos --format='%g: %o%~' foobar.txt
To outputone
,two
, anda word
for the sub-patterns[fF]oo
,[bB]ar
,and any other word\w+
, respectively, using argument[one|two|a word]
withfield%g
indexed by sub-pattern (or group captures with option-P
):
ug --format='%[one|two|a word]g%~' '([fF]oo)|([bB]ar)|(\w+)' foobar.txt
To output a list of group capture indices with%G
separated by the wordand
instead of the default colons with%[ and ]$
, followed by the matching line:
ug -P --format='%[ and ]$%G%$%s%O%~' '(foo)|(ba((r)|(z)))' foobar.txt
Same, but showing names instead of numbers:
ug -P --format='%[ and ]$%[foo|ba|r|z]G%$%s%O%~' '(foo)|(ba(?:(r)|(z)))' foobar.txt
Note that option-P
is required for general use of group captures forsub-patterns. Named sub-pattern matches may be used with PCRE2 and shown inthe output:
ug -P --format='%[ and ]$%G%$%s%O%~' '(?P<foo>foo)|(?P<ba>ba(?:(?P<r>r)|(?P<z>z)))' foobar.txt
--replace=FORMAT Replace matching patterns in the output by the specified FORMAT with `%' fields. If -P is specified, FORMAT may include `%1' to `%9', `%[NUM]#' and `%[NAME]#' to output group captures. A `%%' outputs `%' and `%~' outputs a newline. See option --format, `ugrep --help format' and `man ugrep' section FORMAT for details.-y, --any-line Any line is output (passthru). Non-matching lines are output as context with a `-' separator. See also options -A, -B, and -C.-P, --perl-regexp Interpret PATTERN as a Perl regular expression.--format=FORMAT Output FORMAT-formatted matches. For example --format='%f:%n:%O%~' outputs matching lines `%O' with filename `%f` and line number `%n' followed by a newline `%~'. If -P is specified, FORMAT may include `%1' to `%9', `%[NUM]#' and `%[NAME]#' to output group captures. A `%%' outputs `%'. See `ugrep --help format' and `man ugrep' section FORMAT for details. When option -o is specified, option -u is also enabled. Context options -A, -B, -C and -y are ignored.
Seecustomized output with --format for details on theFORMAT
fields.
For option-o
, the replacement is not automatically followed by a newline toallow for more flexibility in replacements. To output a newline, use%~
intheFORMAT
string.
Use option-P
to use group captures and backreferences. Capturing groups inregex patterns are parenthesized expressions(pattern)
and the first isreferenced inFORMAT
by%1
, the second by%2
and so on. Named capturesare of the form(?<NAME>pattern)
and are referenced inFORMAT
by%[NAME]#
.
To display pattern matches with their sequential match number using--replace='%m:%o'
where%m
is the sequential match number and%o
is thepattern matched:
ug --replace='%m:%o' pattern myfile.txt
Same, but passing the file through with option-y
, while applying thereplacements to the output:
ug -y --replace='%m:%o' pattern myfile.txt
To extract table cells from an HTML file using Perl matching (-P
) to supportgroup captures with lazy quantifier(.*?)
, and translate the matches to acomma-separated list with format%,%1
(conditional comma and group capture):
ug -P -o '<td>(.*?)</td>' --replace='%,%1' index.html
Same, but using--format='%,%1'
instead and we do not need-o
(note that--replace
color-highlights matches shown on a terminal but--format
doesnot):
ug -P '<td>(.*?)</td>' --format='%,%1' index.html
Same, but displaying the formatted matches line-by-line, with--replace
orwith--format
:
ug -P -o '<td>(.*?)</td>' --replace='%,%1' index.htmlug -P '<td>(.*?)</td>' --format='%1%~' index.html
To collect allhref
URLs from all HTML and PHP files down the workingdirectory, then sort them:
ug -r -thtml,php -P '<[^<>]+href\h*=\h*.([^\x27"]+).' --format='%1%~' | sort -u
Same, but much easier by using the predefinedhtml/href
pattern:
ug -r -thtml,php -P -f html/href --format='%1%~' | sort -u
Same, but in this case select<script>
src
URLs when referencinghttp
andhttps
sites:
ug -r -thtml,php -P '<script.*src\h*=\h*.(https?:[^\x27"]+).' --format='%1%~' | sort -u
--depth=[MIN,][MAX], -1, -2, -3, ... -9, -10, -11, -12, ... Restrict recursive searches from MIN to MAX directory levels deep, where -1 (--depth=1) searches the specified path without recursing into subdirectories. Note that -3 -5, -3-5, and -35 search 3 to 5 levels deep. Enables -r if -R or -r is not specified.-K [MIN,][MAX], --range=[MIN,][MAX], --min-line=MIN, --max-line=MAX Start searching at line MIN, stop at line MAX when specified.-m [MIN,][MAX], --min-count=MIN, --max-count=MAX Require MIN matches, stop after MAX matches when specified. Output MIN to MAX matches. For example, -m1 outputs the first match and -cm1, (with a comma) counts nonzero matches. If -u is specified, each individual match counts. See also option -K.--max-files=NUM Restrict the number of files matched to NUM. Note that --sort or -J1 may be specified to produce replicable results. If --sort is specified, the number of threads spawned is limited to NUM.--sort[=KEY] Displays matching files in the order specified by KEY in recursive searches. Normally the ug command sorts by name whereas the ugrep batch command displays matches in no particular order to improve performance. The sort KEY can be `name' to sort by pathname (default), `best' to sort by best match with option -Z (sort by best match requires two passes over files, which is expensive), `size' to sort by file size, `used' to sort by last access time, `changed' to sort by last modification time and `created' to sort by creation time. Sorting is reversed with `rname', `rbest', `rsize', `rused', `rchanged', or `rcreated'. Archive contents are not sorted. Subdirectories are sorted and displayed after matching files. FILE arguments are searched in the same order as specified.
To show only up to the first 10 matching lines withFIXME
in C++ files in theworking directory and all subdirectories below:
ug -r -m10 -tc++ FIXME
Same, but recursively search up to two directory levels, meaning that./
and./sub/
are visited but not deeper:
ug -2 -m10 -tc++ FIXME
To show only the first two files that have one or more matches ofFIXME
inthe list of files sorted by pathname, using--max-files=2
:
ug --sort -r --max-files=2 -tc++ FIXME
To search fileinstall.sh
for the occurrences of the wordmake
after thefirst line, we use-K
with line number 2 to start searching, where-n
showsthe line numbers in the output:
ug -n -K2 -w make install.sh
Same, but restricting the search to lines 2 to 40 (inclusive):
ug -n -K2,40 -w make install.sh
Same, but showing all lines 2 to 40 with-y
:
ug -y -n -K2,40 -w make install.sh
Same, but showing only the first four matching lines after line 2, with oneline of context:
ug -n -C1 -K2 -m4 -w make install.sh
-Y, --empty Permits empty matches. By default, empty matches are disabled, unless a pattern begins with `^' or ends with `$'. Note that -Y when specified with an empty-matching pattern, such as x? and x*, match all input, not only lines containing the character `x'.
Option-Y
permits empty pattern matches, like GNU/BSD grep. This option isintroduced byugrep to prevent accidental matching with empty patterns:empty-matching patterns such asx?
andx*
match all input, not only lineswithx
. By default, without-Y
, patterns match lines with at least onex
as intended.
This option is automatically enabled when a pattern starts with^
or endswith$
is specified. For example,^\h*$
matches blank lines, includingempty lines.
To recursively list files in the working directory with blank lines, i.e. lineswith white space only, including empty lines (note that option-Y
isimplicitly enabled since the pattern starts with^
and ends with$
):
ug -l '^\h*$'
-i, --ignore-case Perform case insensitive matching. By default, ugrep is case sensitive. By default, this option applies to ASCII letters only. Use options -P and -i for Unicode case insensitive matching.-j, --smart-case Perform case insensitive matching like option -i, unless a pattern is specified with a literal ASCII upper case letter.
To matchtodo
inmyfile.cpp
regardless of case:
ug -i 'todo' myfile.txt
To matchtodo XXX
withtodo
in any case butXXX
as given, with pattern(?i:todo)
to matchtodo
ignoring case:
ug '(?i:todo) XXX' myfile.cpp
--sort[=KEY] Displays matching files in the order specified by KEY in recursive searches. Normally the ug command sorts by name whereas the ugrep batch command displays matches in no particular order to improve performance. The sort KEY can be `name' to sort by pathname (default), `best' to sort by best match with option -Z (sort by best match requires two passes over files, which is expensive), `size' to sort by file size, `used' to sort by last access time, `changed' to sort by last modification time and `created' to sort by creation time. Sorting is reversed with `rname', `rbest', `rsize', `rused', `rchanged', or `rcreated'. Archive contents are not sorted. Subdirectories are sorted and displayed after matching files. FILE arguments are searched in the same order as specified.
Matching files are displayed in the order specified by--sort
per directorysearched. By default, theug
command sorts by name whereas the output of theugrep
command is not sorted to improve performance, unless option-Q
isused which sorts files by name. An optimized sorting method and strategy areimplemented in the asynchronous output class to keep the overhead of sortingvery low. Directories are displayed after files are displayed first, whenrecursing, which visually aids the user in finding the "closest" matching filesfirst at the top of the displayed results.
To recursively search for C++ files that matchmain
and sort them by datecreated:
ug --sort=created -tc++ 'main'
Same, but sorted by time changed from most recent to oldest:
ug --sort=rchanged -tc++ 'main'
When searching non-binary files only, the binary content check is disabled withoption-a
(--text
) to speed up searching and displaying pattern matches.For example, searching for lines withint
in C++ source code:
ug -r -a -Ocpp -w 'int'
If a file has potentially many pattern matches, but each match is only one asingle line, then option-u
(--ungroup
) can speed this up:
ug -r -a -u -Opython -w 'def'
Even greater speeds can be achieved with--format
when searching files withmany matches. For example,--format='%O%~'
displays matching lines for eachmatch on that line, while--format='%o%~'
displays the matching part only.Note that the--format
option does not check for binary matches, so theoutput is always "as is". To match text and binary, you can use--format='%C%~'
to display matches formatted as quoted C++ strings withescapes. To display a line at most once (unless option-u
is used), add the%u
(unique) field to the format string, e.g.--format='%u%O%~'
.
For example, to match all words recursively in the working directory with lineand column numbers, where%n
is the line number,%k
is the column number,%o
is the match (only matching), and%~
is a newline:
ug -r --format='%n,%k:%o%~' '\w+'
To search for pattern-o
inscript.sh
using-e
to explicitly specify apattern to prevent pattern-o
from being interpreted as an option:
ug -n -e '-o' script.sh
Alternatively, using--
to end the list of command arguments:
ug -n -- '-o' script.sh
To recursively list all text files (.txt and .md) that do not properly end witha\n
(-o
is required to match\n
or\z
):
ug -L -o -Otext '\n\z'
To list all markdown sections in text files (.text, .txt, .TXT, and .md):
ug -o -ttext -e '^.*(?=\r?\n(===|---))' -e '^#{1,6}\h+.*'
To display multi-line backtick and indented code blocks in markdown files withtheir line numbers, using a lazy quantifier*?
to make the pattern compact:
ug -n -ttext -e '^```(.|\n)*?\n```' -e '^(\t|[ ]{4}).*'
To find mismatched code (a backtick without matching backtick on the same line)in markdown:
ug -n -ttext -e '`[^`]+' -N '`[^`]*`'
UGREP(1) User Commands UGREP(1)NAME ugrep, ug -- file pattern searcherSYNOPSIS ugrep [OPTIONS] [-i] [-Q|PATTERN] [-e PATTERN] [-N PATTERN] [-f FILE] [-F|-G|-P|-Z] [-U] [-m [MIN,][MAX]] [--bool [--files|--lines]] [-r|-R|-1|...|-9|-10|...] [-t TYPES] [-g GLOBS] [--sort[=KEY]] [-l|-c] [-o] [-n] [-k] [-b] [-A NUM] [-B NUM] [-C NUM] [-y] [--color[=WHEN]|--colour[=WHEN]] [--pretty] [--pager[=COMMAND]] [--hexdump|--csv|--json|--xml] [-I] [-z] [--zmax=NUM] [FILE ...]DESCRIPTION The ugrep utility searches any given input files, selecting files and lines that match one or more patterns specified as regular expressions or as fixed strings. A pattern matches multiple input lines when the pattern's regular expression matches one or more newlines. An empty pattern matches every line. Each input line that matches at least one of the patterns is written to the standard output. The ug command is intended for interactive searching, using a .ugrep configuration file located in the working directory or home directory, see CONFIGURATION. ug is equivalent to ugrep --config --pretty --sort to load a .ugrep file, enhance the terminal output, and sort files by name. The ugrep+ and ug+ commands are the same as the ugrep and ug commands, but also use filters to search pdfs, documents, e-books, and image metadata, when the corresponding filter tools are installed. A list of matching files is produced with option -l (--files-with- matches). Option -c (--count) counts the number of matching lines. When combined with option -o, counts the total number of matches. When combined with option -m1, (--min-count=1), skips files with zero matches. The default pattern syntax is an extended form of the POSIX ERE syntax, same as option -E (--extended-regexp). Try ug --help regex for help with pattern syntax and how to use logical connectives to specify Boolean search queries with option -% (--bool) to match lines and -%% (--bool --files) to match files. Options -F (--fixed-strings), -G (--basic- regexp) and -P (--perl-regexp) specify other pattern syntaxes. Option -i (--ignore-case) ignores case in ASCII patterns. When combined with option -P, ignores case in Unicode patterns. Option -j (--smart- case) enables -i only if the search patterns are specified in lower case. Fuzzy (approximate) search is specified with option -Z (--fuzzy) with an optional argument to control character insertions, deletions, and/or substitutions. Try ug --help fuzzy for help with fuzzy search. Note that pattern `.' matches any non-newline character. Pattern `\n' matches a newline character. Multiple lines may be matched with patterns that match one or more newline characters. The empty pattern "" matches all lines. Other empty-matching patterns do not. For example, the pattern `a*' will match one or more a's. Option -Y forces empty matches for compatibility with other grep tools. Option -f FILE matches patterns specified in FILE. By default Unicode patterns are matched. Option -U (--ascii or --binary) disables Unicode matching for ASCII and binary pattern matching. Non- Unicode matching is more efficient. ugrep accepts input of various encoding formats and normalizes the output to UTF-8. When a UTF byte order mark is present in the input, the input is automatically normalized. An input encoding format may be specified with option --encoding. If no FILE arguments are specified and standard input is read from a terminal, recursive searches are performed as if -r is specified. To force reading from standard input, specify `-' as a FILE argument. Directories specified as FILE arguments are searched without recursing deeper into subdirectories, unless -R, -r, or -2...-9 is specified to search subdirectories recursively (up to the specified depth.) Option -I (--ignore-binary) ignores binary files. A binary file is a file with non-text content. A file with zero bytes or invalid UTF formatting is considered binary. Hidden files and directories are ignored in recursive searches. Option -. (--hidden) includes hidden files and directories in recursive searches. To match the names of files to search and the names of directories to recurse, one or more of the following options may be specified. Option -O specifies one or more filename extensions to match. Option -t specifies one or more file types to search (-t list outputs a list of types.) Option -g specifies a gitignore-style glob pattern to match filenames. Option --ignore-files specifies a file with gitignore-style globs to ignore directories and files. Try ug --help globs for help with filename and directory name matching. See also section GLOBBING. Compressed files and archives are searched with option -z (--decompress). When used with option --zmax=NUM, searches the contents of compressed files and archives stored within archives up to NUM levels. A query terminal user interface (TUI) is opened with -Q (--query) to interactively specify search patterns and view search results. A PATTERN argument requires -e PATTERN to start the query TUI with the specified pattern. Output to a terminal for viewing is enhanced with --pretty, which is enabled by default with the ug command. A terminal output pager is enabled with --pager. Customized output is produced with option --format or --replace. Try ug --help format for help with custom formatting of the output. Predefined formats include CSV with option --csv, JSON with option --json, and XML with option --xml. Hexdumps are output with option -X (--hex) or with option --hexdump to customize hexdumps. See also section FORMAT. A `--' signals the end of options; the rest of the parameters are FILE arguments, allowing filenames to begin with a `-' character. Long options may start with `--no-' to disable, when applicable. ug --help WHAT displays help on options related to WHAT. The following options are available: -A NUM, --after-context=NUM Output NUM lines of trailing context after matching lines. Places a --group-separator between contiguous groups of matches. If -o is specified, output the match with context to fit NUM columns after the match or shortens the match. See also options -B, -C and -y. -a, --text Process a binary file as if it were text. This is equivalent to the --binary-files=text option. This option might output binary garbage to the terminal, which can have problematic consequences if the terminal driver interprets some of it as terminal commands. --all, -@ Search all files except hidden: cancel previous file and directory search restrictions and cancel --ignore-binary and --ignore-files when specified. Restrictions specified after this option, i.e. to the right, are still applied. For example, -@I searches all non-binary files and -@. searches all files including hidden files. Note that hidden files and directories are never searched, unless option -. or --hidden is specified. --and [-e] PATTERN Specify additional PATTERN that must match. Additional -e PATTERN following this option is considered an alternative pattern to match, i.e. each -e is interpreted as an OR pattern enclosed within the AND. For example, -e A -e B --and -e C -e D matches lines with (`A' or `B') and (`C' or `D'). Note that multiple -e PATTERN are alternations that bind more tightly together than --and. Option --stats displays the search patterns applied. See also options --not, --andnot, --bool, --files and --lines. --andnot [-e] PATTERN Combines --and --not. See also options --and, --not and --bool. -B NUM, --before-context=NUM Output NUM lines of leading context before matching lines. Places a --group-separator between contiguous groups of matches. If -o is specified, output the match with context to fit NUM columns before the match or shortens the match. See also options -A, -C and -y. -b, --byte-offset The offset in bytes of a pattern match is displayed in front of the respective matched line. When -u is specified, displays the offset for each pattern matched on the same line. Byte offsets are exact for ASCII, UTF-8 and raw binary input. Otherwise, the byte offset in the UTF-8 normalized input is displayed. --binary-files=TYPE Controls searching and reporting pattern matches in binary files. TYPE can be `binary', `without-match`, `text`, `hex` and `with-hex'. The default is `binary' to search binary files and to report a match without displaying the match. `without-match' ignores binary matches. `text' treats all binary files as text, which might output binary garbage to the terminal, which can have problematic consequences if the terminal driver interprets some of it as commands. `hex' reports all matches in hexadecimal. `with-hex' only reports binary matches in hexadecimal, leaving text matches alone. A match is considered binary when matching a zero byte or invalid UTF. Short options are -a, -I, -U, -W and -X. --bool, -%, -%% Specifies Boolean query patterns. A Boolean query pattern is composed of `AND', `OR', `NOT' operators and grouping with `(' `)'. Spacing between subpatterns is the same as `AND', `|' is the same as `OR' and a `-' is the same as `NOT'. The `OR' operator binds more tightly than `AND'. For example, --bool 'A|B C|D' matches lines with (`A' or `B') and (`C' or `D'), --bool 'A -B' matches lines with `A' and not `B'. Operators `AND', `OR', `NOT' require proper spacing. For example, --bool 'A OR B AND C OR D' matches lines with (`A' or `B') and (`C' or `D'), --bool 'A AND NOT B' matches lines with `A' without `B'. Quoted subpatterns are matched literally as strings. For example, --bool 'A "AND"|"OR"' matches lines with `A' and also either `AND' or `OR'. Parentheses are used for grouping. For example, --bool '(A B)|C' matches lines with `A' and `B', or lines with `C'. Note that all subpatterns in a Boolean query pattern are regular expressions, unless -F is specified. Options -E, -F, -G, -P and -Z can be combined with --bool to match subpatterns as strings or regular expressions (-E is the default.) This option does not apply to -f FILE patterns. The double short option -%% enables options --bool --files. Option --stats displays the Boolean search patterns applied. See also options --and, --andnot, --not, --files and --lines. --break Adds a line break between results from different files. This option is enabled by --heading. -C NUM, --context=NUM Output NUM lines of leading and trailing context surrounding each matching line. Places a --group-separator between contiguous groups of matches. If -o is specified, output the match with context to fit NUM columns before and after the match or shortens the match. See also options -A, -B and -y. -c, --count Only a count of selected lines is written to standard output. When -o or -u is specified, counts the number of patterns matched. When -v is specified, counts the number of non-matching lines. When -m1, (with a comma or --min-count=1) is specified, counts only matching files without outputting zero matches. --color[=WHEN], --colour[=WHEN] Mark up the matching text with the colors specified with option --colors or the GREP_COLOR or GREP_COLORS environment variable. WHEN can be `never', `always', or `auto', where `auto' marks up matches only when output on a terminal. The default is `auto'. --colors=COLORS, --colours=COLORS Use COLORS to mark up text. COLORS is a colon-separated list of one or more parameters `sl=' (selected line), `cx=' (context line), `mt=' (matched text), `ms=' (match selected), `mc=' (match context), `fn=' (file name), `ln=' (line number), `cn=' (column number), `bn=' (byte offset), `se=' (separator), `qp=' (TUI prompt), `qe=' (TUI errors), `qr=' (TUI regex), `qm=' (TUI regex meta characters), `ql=' (TUI regex lists and literals), `qb=' (TUI regex braces). Parameter values are ANSI SGR color codes or `k' (black), `r' (red), `g' (green), `y' (yellow), `b' (blue), `m' (magenta), `c' (cyan), `w' (white), or leave empty for no color. Upper case specifies background colors. A `+' qualifies a color as bright. A foreground and a background color may be combined with font properties `n' (normal), `f' (faint), `h' (highlight), `i' (invert), `u' (underline). Parameter `hl' enables file name hyperlinks. Parameter `rv' reverses the `sl=' and `cx=' parameters when option -v is specified. Selectively overrides GREP_COLORS. Legacy grep single parameter codes may be specified, for example --colors='7;32' or --colors=ig to set ms (match selected). --config[=FILE], ---[FILE] Use configuration FILE. The default FILE is `.ugrep'. The working directory is checked first for FILE, then the home directory. The options specified in the configuration FILE are parsed first, followed by the remaining options specified on the command line. The ug command automatically loads a `.ugrep' configuration file, unless --config=FILE or --no-config is specified. --no-config Do not automatically load the default .ugrep configuration file. --no-confirm Do not confirm actions in -Q query TUI. The default is confirm. --cpp Output file matches in C++. See also options --format and -u. --csv Output file matches in CSV. When -H, -n, -k, or -b is specified, additional values are output. See also options --format and -u. -D ACTION, --devices=ACTION If an input file is a device, FIFO or socket, use ACTION to process it. By default, ACTION is `skip', which means that devices are silently skipped. When ACTION is `read', devices read just as if they were ordinary files. -d ACTION, --directories=ACTION If an input file is a directory, use ACTION to process it. By default, ACTION is `skip', i.e., silently skip directories unless specified on the command line. When ACTION is `read', warn when directories are read as input. When ACTION is `recurse', read all files under each directory, recursively, following symbolic links only if they are on the command line. This is equivalent to the -r option. When ACTION is `dereference-recurse', read all files under each directory, recursively, following symbolic links. This is equivalent to the -R option. --delay=DELAY Set the default -Q key response delay. Default is 3 for 300ms. --depth=[MIN,][MAX], -1, -2, -3, ... -9, -10, -11, ... Restrict recursive searches from MIN to MAX directory levels deep, where -1 (--depth=1) searches the specified path without recursing into subdirectories. The short forms -3 -5, -3-5 and -3,5 search 3 to 5 levels deep. Enables -r if -R or -r is not specified. --dotall Dot `.' in regular expressions matches anything, including newline. Note that `.*' matches all input and should not be used. -E, --extended-regexp Interpret patterns as extended regular expressions (EREs). This is the default. -e PATTERN, --regexp=PATTERN Specify a PATTERN to search the input. An input line is selected if it matches any of the specified patterns. This option is useful when multiple -e options are used to specify multiple patterns, or when a pattern begins with a dash (`-'), or to specify a pattern after option -f or after the FILE arguments. --encoding=ENCODING The encoding format of the input. The default ENCODING is binary or UTF-8 which are treated the same. Therefore, --encoding=binary has no effect. Note that option -U or --binary specifies binary PATTERN matching (text matching is the default). ENCODING can be: `binary', `ASCII', `UTF-8', `UTF-16', `UTF-16BE', `UTF-16LE', `UTF-32', `UTF-32BE', `UTF-32LE', `LATIN1', `ISO-8859-1', `ISO-8859-2', `ISO-8859-3', `ISO-8859-4', `ISO-8859-5', `ISO-8859-6', `ISO-8859-7', `ISO-8859-8', `ISO-8859-9', `ISO-8859-10', `ISO-8859-11', `ISO-8859-13', `ISO-8859-14', `ISO-8859-15', `ISO-8859-16', `MAC', `MACROMAN', `EBCDIC', `CP437', `CP850', `CP858', `CP1250', `CP1251', `CP1252', `CP1253', `CP1254', `CP1255', `CP1256', `CP1257', `CP1258', `KOI8-R', `KOI8-U', `KOI8-RU', `null-data'. --exclude=GLOB Exclude files whose name matches GLOB, same as -g ^GLOB. GLOB can use **, *, ?, and [...] as wildcards and \ to quote a wildcard or backslash character literally. When GLOB contains a `/', full pathnames are matched. Otherwise basenames are matched. When GLOB ends with a `/', directories are excluded as if --exclude-dir is specified. Otherwise files are excluded. Note that --exclude patterns take priority over --include patterns. GLOB should be quoted to prevent shell globbing. This option may be repeated. --exclude-dir=GLOB Exclude directories whose name matches GLOB from recursive searches, same as -g ^GLOB/. GLOB can use **, *, ?, and [...] as wildcards and \ to quote a wildcard or backslash character literally. When GLOB contains a `/', full pathnames are matched. Otherwise basenames are matched. Note that --exclude-dir patterns take priority over --include-dir patterns. GLOB should be quoted to prevent shell globbing. This option may be repeated. --exclude-from=FILE Read the globs from FILE and skip files and directories whose name matches one or more globs. A glob can use **, *, ?, and [...] as wildcards and \ to quote a wildcard or backslash character literally. When a glob contains a `/', full pathnames are matched. Otherwise basenames are matched. When a glob ends with a `/', directories are excluded as if --exclude-dir is specified. Otherwise files are excluded. A glob starting with a `!' overrides previously-specified exclusions by including matching files. Lines starting with a `#' and empty lines in FILE are ignored. When FILE is a `-', standard input is read. This option may be repeated. --exclude-fs=MOUNTS Exclude file systems specified by MOUNTS from recursive searches. MOUNTS is a comma-separated list of mount points or pathnames to directories. When MOUNTS is not specified, only descends into the file systems associated with the specified file and directory search targets, i.e. excludes all other file systems. Note that --exclude-fs=MOUNTS take priority over --include-fs=MOUNTS. This option may be repeated. -F, --fixed-strings Interpret pattern as a set of fixed strings, separated by newlines, any of which is to be matched. This makes ugrep behave as fgrep. If a PATTERN is specified, or -e PATTERN or -N PATTERN, then this option has no effect on -f FILE patterns to allow -f FILE patterns to narrow or widen the scope of the PATTERN search. -f FILE, --file=FILE Read newline-separated patterns from FILE. White space in patterns is significant. Empty lines in FILE are ignored. If FILE does not exist, the GREP_PATH environment variable is used as path to FILE. If that fails, looks for FILE in /usr/local/share/ugrep/patterns. When FILE is a `-', standard input is read. Empty files contain no patterns; thus nothing is matched. This option may be repeated. --filter=COMMANDS Filter files through the specified COMMANDS first before searching. COMMANDS is a comma-separated list of `exts:command arguments', where `exts' is a comma-separated list of filename extensions and `command' is a filter utility. Files matching one of `exts' are filtered. A `*' matches any file. The specified `command' may include arguments separated by spaces. An argument may be quoted to include spacing, commas or a `%'. A `%' argument expands into the pathname to search. For example, --filter='pdf:pdftotext % -' searches PDF files. The `%' expands into a `-' when searching standard input. When a `%' is not specified, the filter command should read from standard input and write to standard output. Option --label=.ext may be used to specify extension `ext' when searching standard input. This option may be repeated. --filter-magic-label=[+]LABEL:MAGIC Associate LABEL with files whose signature "magic bytes" match the MAGIC regex pattern. Only files that have no filename extension are labeled, unless +LABEL is specified. When LABEL matches an extension specified in --filter=COMMANDS, the corresponding command is invoked. This option may be repeated. --format=FORMAT Output FORMAT-formatted matches. For example --format='%f:%n:%O%~' outputs matching lines `%O' with filename `%f` and line number `%n' followed by a newline `%~'. If -P is specified, FORMAT may include `%1' to `%9', `%[NUM]#' and `%[NAME]#' to output group captures. A `%%' outputs `%'. See `ugrep --help format' and `man ugrep' section FORMAT for details. When option -o is specified, option -u is also enabled. Context options -A, -B, -C and -y are ignored. --free-space Spacing (blanks and tabs) in regular expressions are ignored. -G, --basic-regexp Interpret patterns as basic regular expressions (BREs). -g GLOBS, --glob=GLOBS, --iglob=GLOBS Only search files whose name matches the specified comma-separated list of GLOBS, same as --include=glob for each `glob' in GLOBS. When a `glob' is preceded by a `!' or a `^', skip files whose name matches `glob', same as --exclude='glob'. When `glob' contains a `/', full pathnames are matched. Otherwise basenames are matched. When `glob' ends with a `/', directories are matched, same as --include-dir='glob' and --exclude-dir='glob'. A leading `/' matches the working directory. Option --iglob performs case-insensitive name matching. This option may be repeated and may be combined with options -M, -O and -t. For more details, see `ugrep --help globs' and `man ugrep' section GLOBBING for details. --glob-ignore-case Perform case-insensitive glob matching in general. --group-separator[=SEP] Use SEP as a group separator for context options -A, -B and -C. The default is a double hyphen (`--'). --no-group-separator Removes the group separator line from the output for context options -A, -B and -C. -H, --with-filename Always print the filename with output lines. This is the default when there is more than one file to search. -h, --no-filename Never print filenames with output lines. This is the default when there is only one file (or only standard input) to search. --heading, -+ Group matches per file. Adds a heading and a line break between results from different files. This option is enabled by --pretty when the output is sent to a terminal. --help [WHAT], -? [WHAT] Display a help message on options related to WHAT when specified. In addition, `--help regex' displays an overview of regular expressions, `--help globs' displays an overview of glob syntax and conventions, `--help fuzzy' displays details of fuzzy search, and `--help format' displays a list of option --format=FORMAT fields. --hexdump[=[1-8][a][bch][A[NUM]][B[NUM]][C[NUM]]] Output matches in 1 to 8 columns of 8 hexadecimal octets. The default is 2 columns or 16 octets per line. Argument `a' outputs a `*' for all hex lines that are identical to the previous hex line, `b' removes all space breaks, `c' removes the character column, `h' removes hex spacing, `A' includes up to NUM hex lines after a match, `B' includes up to NUM hex lines before a match and `C' includes up to NUM hex lines before and after a match. Arguments `A', `B' and `C' are the same as options -A, -B and -C when used with --hexdump. See also options -U, -W and -X. --hidden, -. Search hidden files and directories (enabled by default in grep compatibility mode). --hyperlink[=[PREFIX][+]] Hyperlinks are enabled for file names when colors are enabled. Same as --colors=hl. When PREFIX is specified, replaces file:// with PREFIX:// in the hyperlink. A `+' includes the line number in the hyperlink and when option -k is specified, the column number. -I, --ignore-binary Ignore matches in binary files. This option is equivalent to the --binary-files=without-match option. -i, --ignore-case Perform case insensitive matching. By default, ugrep is case sensitive. By default, this option applies to ASCII letters only. Use options -P and -i for Unicode case insensitive matching. --ignore-files[=FILE] Ignore files and directories matching the globs in each FILE that is encountered in recursive searches. The default FILE is `.gitignore'. Matching files and directories located in the directory of the FILE and in subdirectories below are ignored. Globbing syntax is the same as the --exclude-from=FILE gitignore syntax, but files and directories are excluded instead of only files. Directories are specifically excluded when the glob ends in a `/'. Files and directories explicitly specified as command line arguments are never ignored. This option may be repeated to specify additional files. --no-ignore-files Do not ignore files, i.e. cancel --ignore-files when specified. --include=GLOB Only search files whose name matches GLOB, same as -g GLOB. GLOB can use **, *, ?, and [...] as wildcards and \ to quote a wildcard or backslash character literally. When GLOB contains a `/', full pathnames are matched. Otherwise basenames are matched. When GLOB ends with a `/', directories are included as if --include-dir is specified. Otherwise files are included. Note that --exclude patterns take priority over --include patterns. GLOB should be quoted to prevent shell globbing. This option may be repeated. --include-dir=GLOB Only directories whose name matches GLOB are included in recursive searches, same as -g GLOB/. GLOB can use **, *, ?, and [...] as wildcards and \ to quote a wildcard or backslash character literally. When GLOB contains a `/', full pathnames are matched. Otherwise basenames are matched. Note that --exclude-dir patterns take priority over --include-dir patterns. GLOB should be quoted to prevent shell globbing. This option may be repeated. --include-from=FILE Read the globs from FILE and search only files and directories whose name matches one or more globs. A glob can use **, *, ?, and [...] as wildcards and \ to quote a wildcard or backslash character literally. When a glob contains a `/', full pathnames are matched. Otherwise basenames are matched. When a glob ends with a `/', directories are included as if --include-dir is specified. Otherwise files are included. A glob starting with a `!' overrides previously-specified inclusions by excluding matching files. Lines starting with a `#' and empty lines in FILE are ignored. When FILE is a `-', standard input is read. This option may be repeated. --include-fs=MOUNTS Only file systems specified by MOUNTS are included in recursive searches. MOUNTS is a comma-separated list of mount points or pathnames to directories. When MOUNTS is not specified, restricts recursive searches to the file system of the working directory, same as --include-fs=. (dot). Note that --exclude-fs=MOUNTS take priority over --include-fs=MOUNTS. This option may be repeated. --index Perform fast index-based recursive search. This option assumes, but does not require, that files are indexed with ugrep-indexer. This option also enables option -r or --recursive. Skips indexed non-matching files, archives and compressed files. Significant acceleration may be achieved on cold (not file-cached) and large file systems, or any file system that is slow to search. Note that the start-up time to search may be increased when complex search patterns are specified that contain large Unicode character classes combined with `*' or `+' repeats, which should be avoided. Option -U (--ascii) improves performance. Option --stats displays an index search report. -J NUM, --jobs=NUM Specifies the number of threads spawned to search files. By default an optimum number of threads is spawned to search files simultaneously. -J1 disables threading: files are searched in the same order as specified. -j, --smart-case Perform case insensitive matching, unless a pattern is specified with a literal upper case ASCII letter. --json Output file matches in JSON. When -H, -n, -k, or -b is specified, additional values are output. See also options --format and -u. -K [MIN,][MAX], --range=[MIN,][MAX], --min-line=MIN, --max-line=MAX Start searching at line MIN, stop at line MAX when specified. -k, --column-number The column number of a pattern match is displayed in front of the respective matched line, starting at column 1. Tabs are expanded in counting columns, see also option --tabs. -L, --files-without-match Only the names of files not containing selected lines are written to standard output. Pathnames are listed once per file searched. If the standard input is searched, the string ``(standard input)'' is written. -l, --files-with-matches Only the names of files containing selected lines are written to standard output. ugrep will only search a file until a match has been found, making searches potentially less expensive. Pathnames are listed once per file searched. If the standard input is searched, the string ``(standard input)'' is written. --label=LABEL Displays the LABEL value when input is read from standard input where a file name would normally be printed in the output. Associates a filename extension with standard input when LABEL has a suffix. The default value is `(standard input)'. --line-buffered Force output to be line buffered instead of block buffered. --lines Boolean line matching mode for option --bool, the default mode. -M MAGIC, --file-magic=MAGIC Only search files matching the magic signature pattern MAGIC. The signature "magic bytes" at the start of a file are compared to the MAGIC regex pattern. When matching, the file will be searched. When MAGIC is preceded by a `!' or a `^', skip files with matching MAGIC signatures. This option may be repeated and may be combined with options -O and -t. Every file on the search path is read, making recursive searches potentially more expensive. -m [MIN,][MAX], --min-count=MIN, --max-count=MAX Require MIN matches, stop after MAX matches when specified. Output MIN to MAX matches. For example, -m1 outputs the first match and -cm1, (with a comma) counts nonzero matches. When -u or --ungroup is specified, each individual match counts. See also option -K. --match Match all input. Same as specifying an empty pattern to search. --max-files=NUM Restrict the number of files matched to NUM. Note that --sort or -J1 may be specified to produce replicable results. If --sort is specified, then the number of threads spawned is limited to NUM. --mmap[=MAX] Use memory maps to search files. By default, memory maps are used under certain conditions to improve performance. When MAX is specified, use up to MAX mmap memory per thread. -N PATTERN, --neg-regexp=PATTERN Specify a negative PATTERN to reject specific -e PATTERN matches with a counter pattern. Note that longer patterns take precedence over shorter patterns, i.e. a negative pattern must be of the same length or longer to reject matching patterns. Option -N cannot be specified with -P. This option may be repeated. -n, --line-number Each output line is preceded by its relative line number in the file, starting at line 1. The line number counter is reset for each file processed. --not [-e] PATTERN Specifies that PATTERN should not match. Note that -e A --not -e B matches lines with `A' or lines without a `B'. To match lines with `A' that have no `B', specify -e A --andnot -e B. Option --stats displays the search patterns applied. See also options --and, --andnot, --bool, --files and --lines. --null, -0 Output a zero byte after the file name. This option can be used with commands such as `find -print0' and `xargs -0' to process arbitrary file names, even those that contain newlines. See also options -H or --with-filename and --null-data. --null-data, -00 Input and output are treated as sequences of lines with each line terminated by a zero byte instead of a newline; effectively swaps NUL with LF in the input and the output. When combined with option --encoding=ENCODING, output each line terminated by a zero byte without affecting the input specified as per ENCODING. Instead of option --null-data, option --encoding=null-data treats the input as a sequence of lines terminated by a zero byte without affecting the output. Option --null-data is not compatible with UTF-16/32 input. See also options --encoding and --null. -O EXTENSIONS, --file-extension=EXTENSIONS Only search files whose filename extensions match the specified comma-separated list of EXTENSIONS, same as -g '*.ext' for each `ext' in EXTENSIONS. When an `ext' is preceded by a `!' or a `^', skip files whose filename extensions matches `ext', same as -g '^*.ext'. This option may be repeated and may be combined with options -g, -M and -t. -o, --only-matching Only the matching part of a pattern match is output. When -A, -B or -C is specified, fits the match and its context on a line within the specified number of columns. --only-line-number Only the line number of a matching line is output. The line number counter is reset for each file processed. --files, -%% Boolean file matching mode, the opposite of --lines. When combined with option --bool, matches a file if all Boolean conditions are satisfied. For example, --bool --files 'A B|C -D' matches a file if some lines match `A', and some lines match either `B' or `C', and no line matches `D'. See also options --and, --andnot, --not, --bool and --lines. The double short option -%% enables options --bool --files. -P, --perl-regexp Interpret PATTERN as a Perl regular expression using PCRE2. Note that Perl pattern matching differs from the default grep POSIX pattern matching. -p, --no-dereference If -R or -r is specified, do not follow symbolic links, even when symbolic links are specified on the command line. --pager[=COMMAND] When output is sent to the terminal, uses COMMAND to page through the output. COMMAND defaults to environment variable PAGER when defined or `less'. Enables --heading and --line-buffered. --pretty[=WHEN] When output is sent to a terminal, enables --color, --heading, -n, --sort, --tree and -T when not explicitly disabled. WHEN can be `never', `always', or `auto'. The default is `auto'. -Q[=DELAY], --query[=DELAY] Query mode: start a TUI to perform interactive searches. This mode requires an ANSI capable terminal. An optional DELAY argument may be specified to reduce or increase the response time to execute searches after the last key press, in increments of 100ms, where the default is 3 (300ms delay). No whitespace may be given between -Q and its argument DELAY. Initial patterns may be specified with -e PATTERN, i.e. a PATTERN argument requires option -e. Press F1 or CTRL-Z to view the help screen. Press F2 or CTRL-Y to invoke a command to view or edit the file shown at the top of the screen. The command can be specified with option --view and defaults to environment variable PAGER when defined, or VISUAL or EDITOR. Press TAB or SHIFT-TAB to navigate directories and to select a file to search. Press ENTER to select lines to output. Press ALT-l for option -l to list files, ALT-n for -n, etc. Non-option commands include ALT-] to increase context and ALT-} to increase fuzzyness. If ALT or OPTION keys are not available, then press CTRL-O + KEY to switch option `KEY', or press F1 or CTRL-Z for help and press KEY. See also options --no-confirm, --delay, --split and --view. -q, --quiet, --silent Quiet mode: suppress all output. Only search a file until a match has been found. -R, --dereference-recursive Recursively read all files under each directory, following symbolic links to files and directories, unlike -r. -r, --recursive Recursively read all files under each directory, following symbolic links only if they are on the command line. Note that when no FILE arguments are specified and input is read from a terminal, recursive searches are performed as if -r is specified. --replace=FORMAT Replace matching patterns in the output by FORMAT with `%' fields. If -P is specified, FORMAT may include `%1' to `%9', `%[NUM]#' and `%[NAME]#' to output group captures. A `%%' outputs `%' and `%~' outputs a newline. See also option --format, `ugrep --help format' and `man ugrep' section FORMAT for details. -S, --dereference-files When -r is specified, follow symbolic links to files, but not to directories. The default is not to follow symbolic links. -s, --no-messages Silent mode: nonexistent and unreadable files are ignored and their error messages and warnings are suppressed. --save-config[=FILE] [OPTIONS] Save configuration FILE to include OPTIONS. Update FILE when first loaded with --config=FILE. The default FILE is `.ugrep', which is automatically loaded by the ug command. When FILE is a `-', writes the configuration to standard output. Only part of the OPTIONS are saved that do not cause searches to fail when combined with other options. Additional options may be specified by editing the saved configuration file. A configuration file may be modified manually to specify one or more config[=FILE] to indirectly load the specified FILE, but recursive config loading is not allowed. --separator[=SEP], --context-separator=SEP Use SEP as field separator between file name, line number, column number, byte offset and the matched line. The default separator is a colon (`:') and a bar (`|') for multi-line pattern matches, and a dash (`-') for context lines. See also option --group-separator. --split Split the -Q query TUI screen on startup. --sort[=KEY] Displays matching files in the order specified by KEY in recursive searches. Normally the ug command sorts by name whereas the ugrep batch command displays matches in no particular order to improve performance. The sort KEY can be `name' to sort by pathname (default), `best' to sort by best match with option -Z (sort by best match requires two passes over files, which is expensive), `size' to sort by file size, `used' to sort by last access time, `changed' to sort by last modification time and `created' to sort by creation time. Sorting is reversed with `rname', `rbest', `rsize', `rused', `rchanged', or `rcreated'. Archive contents are not sorted. Subdirectories are sorted and displayed after matching files. FILE arguments are searched in the same order as specified. --stats Output statistics on the number of files and directories searched and the inclusion and exclusion constraints applied. -T, --initial-tab Add a tab space to separate the file name, line number, column number and byte offset with the matched line. -t TYPES, --file-type=TYPES Search only files associated with TYPES, a comma-separated list of file types. Each file type corresponds to a set of filename extensions passed to option -O and filenames passed to option -g. For capitalized file types, the search is expanded to include files with matching file signature magic bytes, as if passed to option -M. When a type is preceded by a `!' or a `^', excludes files of the specified type. Specifying the initial part of a type name suffices when the choice is unambiguous. This option may be repeated. The possible file types can be (-tlist displays a list): `actionscript', `ada', `adoc', `asm', `asp', `aspx', `autoconf', `automake', `awk', `Awk', `basic', `batch', `bison', `c', `c++', `clojure', `cpp', `csharp', `css', `csv', `dart', `Dart', `delphi', `elisp', `elixir', `erlang', `fortran', `gif', `Gif', `go', `groovy', `gsp', `haskell', `html', `jade', `java', `jpeg', `Jpeg', `js', `json', `jsp', `julia', `kotlin', `less', `lex', `lisp', `lua', `m4', `make', `markdown', `matlab', `node', `Node', `objc', `objc++', `ocaml', `parrot', `pascal', `pdf', `Pdf', `perl', `Perl', `php', `Php', `png', `Png', `prolog', `python', `Python', `r', `rpm', `Rpm', `rst', `rtf', `Rtf', `ruby', `Ruby', `rust', `scala', `scheme', `shell', `Shell', `smalltalk', `sql', `svg', `swift', `tcl', `tex', `text', `tiff', `Tiff', `tt', `typescript', `verilog', `vhdl', `vim', `xml', `Xml', `yacc', `yaml', `zig'. --tabs[=NUM] Set the tab size to NUM to expand tabs for option -k. The value of NUM may be 1 (no expansion), 2, 4, or 8. The default size is 8. --tag[=TAG[,END]] Disables colors to mark up matches with TAG. END marks the end of a match if specified, otherwise TAG. The default is `___'. --tree, -^ Output directories with matching files in a tree-like format for option -c or --count, -l or --files-with-matches, -L or --files-without-match. This option is enabled by --pretty when the output is sent to a terminal. -U, --ascii, --binary Disables Unicode matching for ASCII and binary matching. PATTERN matches bytes, not Unicode characters. For example, -U '\xa3' matches byte A3 (hex) instead of the Unicode code point U+00A3 represented by the UTF-8 sequence C2 A3. See also option --dotall. -u, --ungroup Do not group multiple pattern matches on the same matched line. Output the matched line again for each additional pattern match. -V, --version Display version with linked libraries and exit. -v, --invert-match Selected lines are those not matching any of the specified patterns. --view[=COMMAND] Use COMMAND to view/edit a file in -Q query TUI by pressing CTRL-Y. -W, --with-hex Output binary matches in hexadecimal, leaving text matches alone. This option is equivalent to the --binary-files=with-hex option. To omit the matching line from the hex output, use both options -W and --hexdump. See also options -U. -w, --word-regexp The PATTERN is searched for as a word, such that the matching text is preceded by a non-word character and is followed by a non-word character. Word-like characters are Unicode letters, digits and connector punctuations such as underscore. --width[=NUM] Truncate the output to NUM visible characters per line. The width of the terminal window is used if NUM is not specified. Note that double-width characters in the output may result in wider lines. -X, --hex Output matches and matching lines in hexadecimal. This option is equivalent to the --binary-files=hex option. To omit the matching line from the hex output use option --hexdump. See also option -U. -x, --line-regexp Select only those matches that exactly match the whole line, as if the patterns are surrounded by ^ and $. --xml Output file matches in XML. When -H, -n, -k, or -b is specified, additional values are output. See also options --format and -u. -Y, --empty Empty-matching patterns match all lines. Normally, empty matches are not output, unless a pattern begins with `^' or ends with `$'. With this option, empty-matching patterns, such as x? and x*, match all lines, not only lines with an `x' (enabled by default in grep compatibility mode). -y, --any-line, --passthru Any line is output (passthru). Non-matching lines are output as context with a `-' separator. See also options -A, -B and -C. -Z[best][+-~][MAX], --fuzzy[=[best][+-~][MAX]] Fuzzy mode: report approximate pattern matches within MAX errors. The default is -Z1: one deletion, insertion or substitution is allowed. If `+`, `-' and/or `~' is specified, then `+' allows insertions, `-' allows deletions and `~' allows substitutions. For example, -Z+~3 allows up to three insertions or substitutions, but no deletions. If `best' is specified, then only the best matching lines are output with the lowest cost per file. Option -Zbest requires two passes over a file and cannot be used with standard input or Boolean queries. Option --sort=best orders matching files by best match. The first character of an approximate match always matches a character at the beginning of the pattern. To fuzzy match the first character, replace it with a `.' or `.?'. Option -U applies fuzzy matching to ASCII and bytes instead of Unicode text. No whitespace may be given between -Z and its argument. -z, --decompress Search compressed files and archives. Archives (.cpio, .pax, .tar) and compressed archives (e.g. .zip, .7z, .taz, .tgz, .tpz, .tbz, .tbz2, .tb2, .tz2, .tlz, .txz, .tzst) are searched and matching pathnames of files in archives are output in braces. When used with option --zmax=NUM, searches the contents of compressed files and archives stored within archives up to NUM levels. When -g, -O, -M, or -t is specified, searches files stored in archives whose filenames match globs, match filename extensions, match file signature magic bytes, or match file types, respectively. Supported compression formats: gzip (.gz), compress (.Z), zip, 7z, bzip2 (requires suffix .bz, .bz2, .bzip2, .tbz, .tbz2, .tb2, .tz2), lzma and xz (requires suffix .lzma, .tlz, .xz, .txz), lz4 (requires suffix .lz4), zstd (requires suffix .zst, .zstd, .tzst), brotli (requires suffix .br), bzip3 (requires suffix .bz3). --zmax=NUM When used with option -z or --decompress, searches the contents of compressed files and archives stored within archives by up to NUM expansion stages. The default --zmax=1 only permits searching uncompressed files stored in cpio, pax, tar, zip and 7z archives; compressed files and archives are detected as binary files and are effectively ignored. Specify --zmax=2 to search compressed files and archives stored in cpio, pax, tar, zip and 7z archives. NUM may range from 1 to 99 for up to 99 decompression and de-archiving steps. Increasing NUM values gradually degrades performance.EXIT STATUS The ugrep utility exits with one of the following values: 0 One or more lines were selected. 1 No lines were selected. >1 An error occurred. If -q or --quiet or --silent is used and a line is selected, the exit status is 0 even if an error occurred.CONFIGURATION The ug command is intended for context-dependent interactive searching and is equivalent to the ugrep --config --pretty --sort command to load the default configuration file `.ugrep' when present in the working directory or in the home directory. A configuration file contains `NAME=VALUE' pairs per line, where `NAME` is the name of a long option (without `--') and `=VALUE' is an argument, which is optional and may be omitted depending on the option. Empty lines and lines starting with a `#' are ignored. The --config=FILE option and its abbreviated form ---FILE load the specified configuration file located in the working directory or, when not found, located in the home directory. An error is produced when FILE is not found or cannot be read. Command line options are parsed in the following order: the configuration file is loaded first, followed by the remaining options and arguments on the command line. The --save-config option saves a `.ugrep' configuration file to the working directory with a subset of the options specified on the command line. The --save-config=FILE option saves the configuration to FILE. The configuration is written to standard output when FILE is a `-'.GLOBBING Globbing is used by options -g, --include, --include-dir, --include-from, --exclude, --exclude-dir, --exclude-from and --ignore-files to match pathnames and basenames in recursive searches. Glob arguments for these options should be quoted to prevent shell globbing. Globbing supports gitignore syntax and the corresponding matching rules, except that a glob normally matches files but not directories. If a glob ends in a path separator `/', then it matches directories but not files, as if --include-dir or --exclude-dir is specified. When a glob contains a path separator `/', the full pathname is matched. Otherwise the basename of a file or directory is matched. For example, *.h matches foo.h and bar/foo.h. bar/*.h matches bar/foo.h but not foo.h and not bar/bar/foo.h. Use a leading `/' to force /*.h to match foo.h but not bar/foo.h. When a glob starts with a `^' or a `!' as in -g^GLOB, the match is negated. Likewise, a `!' (but not a `^') may be used with globs in the files specified --include-from, --exclude-from, and --ignore-files to negate the glob match. Empty lines or lines starting with a `#' are ignored. Glob Syntax and Conventions * Matches anything except /. ? Matches any one character except /. [abc-e] Matches one character a,b,c,d,e. [^abc-e] Matches one character not a,b,c,d,e,/. [!abc-e] Matches one character not a,b,c,d,e,/. / When used at the start of a glob, matches if pathname has no /. When used at the end of a glob, matches directories only. **/ Matches zero or more directories. /** When used at the end of a glob, matches everything after the /. \? Matches a ? or any other character specified after the backslash. Glob Matching Examples * Matches a, b, x/a, x/y/b a Matches a, x/a, x/y/a, but not b, x/b, a/a/b /* Matches a, b, but not x/a, x/b, x/y/a /a Matches a, but not x/a, x/y/a a?b Matches axb, ayb, but not a, b, ab, a/b a[xy]b Matches axb, ayb but not a, b, azb a[a-z]b Matches aab, abb, acb, azb, but not a, b, a3b, aAb, aZb a[^xy]b Matches aab, abb, acb, azb, but not a, b, axb, ayb a[^a-z]b Matches a3b, aAb, aZb but not a, b, aab, abb, acb, azb a/*/b Matches a/x/b, a/y/b, but not a/b, a/x/y/b **/a Matches a, x/a, x/y/a, but not b, x/b. a/**/b Matches a/b, a/x/b, a/x/y/b, but not x/a/b, a/b/x a/** Matches a/x, a/y, a/x/y, but not a, b/x a\?b Matches a?b, but not a, b, ab, axb, a/b Note that exclude glob patterns take priority over include glob patterns when specified with options -g, --exclude, --exclude-dir, --include and include-dir. Glob patterns specified with prefix `!' in any of the files associated with --include-from, --exclude-from and --ignore-files will negate a previous glob match. That is, any matching file or directory excluded by a previous glob pattern specified in the files associated with --exclude- from or --ignore-file will become included again. Likewise, any matching file or directory included by a previous glob pattern specified in the files associated with --include-from will become excluded again.ENVIRONMENT GREP_PATH May be used to specify a file path to pattern files. The file path is used by option -f to open a pattern file, when the pattern file does not exist. GREP_COLOR May be used to specify ANSI SGR parameters to highlight matches when option --color is used, e.g. 1;35;40 shows pattern matches in bold magenta text on a black background. Deprecated in favor of GREP_COLORS, but still supported. GREP_COLORS May be used to specify ANSI SGR parameters to highlight matches and other attributes when option --color is used. Its value is a colon-separated list of ANSI SGR parameters that defaults to cx=33:mt=1;31:fn=1;35:ln=1;32:cn=1;32:bn=1;32:se=36 with additional parameters for TUI colors :qp=1;32:qe=1;37;41:qm=1;32:ql=36:qb=1;35. The mt=, ms=, and mc= capabilities of GREP_COLORS take priority over GREP_COLOR. Option --colors takes priority over GREP_COLORS.GREP_COLORS Colors are specified as string of colon-separated ANSI SGR parameters of the form `what=substring', where `substring' is a semicolon-separated list of ANSI SGR codes or `k' (black), `r' (red), `g' (green), `y' (yellow), `b' (blue), `m' (magenta), `c' (cyan), `w' (white). Upper case specifies background colors. A `+' qualifies a color as bright. A foreground and a background color may be combined with one or more font properties `n' (normal), `f' (faint), `h' (highlight), `i' (invert), `u' (underline). Substrings may be specified for: sl= selected lines. cx= context lines. rv swaps the sl= and cx= capabilities when -v is specified. mt= matching text in any matching line. ms= matching text in a selected line. The substring mt= by default. mc= matching text in a context line. The substring mt= by default. fn= filenames. ln= line numbers. cn= column numbers. bn= byte offsets. se= separators. rv a Boolean parameter, switches sl= and cx= with option -v. hl a Boolean parameter, enables filename hyperlinks (\33]8;;link). ne a Boolean parameter, disables ``erase in line'' \33[K. qp= TUI prompt. qe= TUI errors. qr= TUI regex. qm= TUI regex meta characters. ql= TUI regex lists and literals. qb= TUI regex braces.FORMAT Option --format=FORMAT specifies an output format for file matches. Fields may be used in FORMAT, which expand into the following values: %[TEXT]F if option -H is used: TEXT, the file pathname and separator. %f the file pathname. %a the file basename without directory path. %p the directory path to the file. %z the file pathname in a (compressed) archive. %[TEXT]H if option -H is used: TEXT, the quoted pathname and separator, \" and \\ replace " and \. %h the quoted file pathname, \" and \\ replace " and \. %[TEXT]I if option -H is used: TEXT, the pathname as XML character data and separator. %i the file pathname as XML character data. %[TEXT]N if option -n is used: TEXT, the line number and separator. %n the line number of the match. %[TEXT]K if option -k is used: TEXT, the column number and separator. %k the column number of the match. %[TEXT]B if option -b is used: TEXT, the byte offset and separator. %b the byte offset of the match. %[TEXT]T if option -T is used: TEXT and a tab character. %t a tab character. %[SEP]$ set field separator to SEP for the rest of the format fields. %[TEXT]< if the first match: TEXT. %[TEXT]> if not the first match: TEXT. %, if not the first match: a comma, same as %[,]>. %: if not the first match: a colon, same as %[:]>. %; if not the first match: a semicolon, same as %[;]>. %| if not the first match: a vertical bar, same as %[|]>. %[TEXT]S if not the first match: TEXT and separator, see also %[SEP]$. %s the separator, see also %[TEXT]S and %[SEP]$. %~ a newline character. %M the number of matching lines %m the number of matches %O the matching line is output as a raw string of bytes. %o the match is output as a raw string of bytes. %Q the matching line as a quoted string, \" and \\ replace " and \. %q the match as a quoted string, \" and \\ replace " and \. %C the matching line formatted as a quoted C/C++ string. %c the match formatted as a quoted C/C++ string. %J the matching line formatted as a quoted JSON string. %j the match formatted as a quoted JSON string. %V the matching line formatted as a quoted CSV string. %v the match formatted as a quoted CSV string. %X the matching line formatted as XML character data. %x the match formatted as XML character data. %w the width of the match, counting wide characters. %d the size of the match, counting bytes. %e the ending byte offset of the match. %Z the edit distance cost of an approximate match with option -Z %u select unique lines only, unless option -u is used. %1 the first regex group capture of the match, and so on up to group %9, same as %[1]#; requires option -P. %[NUM]# the regex group capture NUM; requires option -P. %[NUM]b the byte offset of the group capture NUM; requires option -P. Use e for the ending byte offset and d for the byte length. %[NUM1|NUM2|...]# the first group capture NUM that matched; requires option -P. %[NUM1|NUM2|...]b the byte offset of the first group capture NUM that matched; requires option -P. Use e for the ending byte offset and d for the byte length. %[NAME]# the NAMEd group capture; requires option -P and capturing pattern `(?<NAME>PATTERN)', see also %G. %[NAME]b the byte offset of the NAMEd group capture; requires option -P and capturing pattern `(?<NAME>PATTERN)'. Use e for the ending byte offset and d for the byte length. %[NAME1|NAME2|...]# the first NAMEd group capture that matched; requires option -P and capturing pattern `(?<NAME>PATTERN)', see also %G. %[NAME1|NAME2|...]b the byte offset of the first NAMEd group capture that matched; requires option -P and capturing pattern `(?<NAME>PATTERN)'. Use e for the ending byte offset and d for the byte length. %G list of group capture indices/names that matched; requires option -P. %[TEXT1|TEXT2|...]G list of TEXT indexed by group capture indices that matched; requires option -P. %g the group capture index/name matched or 1; requires option -P. %[TEXT1|TEXT2|...]g the first TEXT indexed by the first group capture index that matched; requires option -P. %% the percentage sign. Formatted output is written without a terminating newline, unless %~ or `\n' is explicitly specified in the format string. The [TEXT] part of a field is optional and may be omitted. When present, the argument must be placed in [] brackets, for example %[,]F to output a comma, the pathname, and a separator. %[SEP]$ and %u are switches and do not send anything to the output. The separator used by the %F, %H, %I, %N, %K, %B, %S and %G fields may be changed by preceding the field by %[SEP]$. When [SEP] is not provided, this reverts the separator to the default separator or the separator specified with --separator. Formatted output is written for each matching pattern, which means that a line may be output multiple times when patterns match more than once on the same line. If field %u is specified anywhere in a format string, matching lines are output only once, unless option -u, --ungroup is specified or when more than one line of input matched the search pattern. Additional formatting options: --format-begin=FORMAT the FORMAT when beginning the search. --format-open=FORMAT the FORMAT when opening a file and a match was found. --format-close=FORMAT the FORMAT when closing a file and a match was found. --format-end=FORMAT the FORMAT when ending the search. The context options -A, -B, -C, -y, and display options --break, --heading, --color, -T, and --null have no effect on formatted output.EXAMPLES Display lines containing the word `patricia' in `myfile.txt': $ ugrep -w patricia myfile.txt Display lines containing the word `patricia', ignoring case: $ ugrep -wi patricia myfile.txt Display lines approximately matching the word `patricia', ignoring case and allowing up to 2 spelling errors using fuzzy search: $ ugrep -Z2 -wi patricia myfile.txt Count the number of lines containing `patricia', ignoring case: $ ugrep -cwi patricia myfile.txt Count the number of words `patricia', ignoring case: $ ugrep -cowi patricia myfile.txt List lines with `amount' and a decimal, ignoring case (space is AND): $ ugrep -i -% 'amount +(.+)?' myfile.txt Alternative query: $ ugrep -wi -e amount --and '+(.+)?' myfile.txt List all Unicode words in a file: $ ugrep -o '\w+' myfile.txt List the laughing face emojis (Unicode code points U+1F600 to U+1F60F): $ ugrep -o '[\x{1F600}-\x{1F60F}]' myfile.txt Check if a file contains any non-ASCII (i.e. Unicode) characters: $ ugrep -q '[^[:ascii:]]' myfile.txt && echo "contains Unicode" Display the line and column number of `FIXME' in C++ files using recursive search, with one line of context before and after a matched line: $ ugrep -C1 -R -n -k -tc++ FIXME Display the line and column number of `FIXME' in long Javascript files using recursive search, showing only matches with up to 10 characters of context before and after: $ ugrep -o -C20 -R -n -k -tjs FIXME Find blocks of text between lines matching BEGIN and END by using a lazy quantifier `*?' to match only what is necessary and pattern `\n' to match newlines: $ ugrep -n 'BEGIN.*\n(.*\n)*?.*END' myfile.txt Likewise, list the C/C++ comments in a file and line numbers: $ ugrep -n -e '//.*' -e '/\*(.*\n)*?.*\*+\/' myfile.cpp The same, but using predefined pattern c++/comments: $ ugrep -n -f c++/comments myfile.cpp List the lines that need fixing in a C/C++ source file by looking for the word `FIXME' while skipping any `FIXME' in quoted strings: $ ugrep -e FIXME -N '"(\\.|\\\r?\n|[^\\\n"])*"' myfile.cpp The same, but using predefined pattern cpp/zap_strings: $ ugrep -e FIXME -f cpp/zap_strings myfile.cpp Find lines with `FIXME' or `TODO', showing line numbers: $ ugrep -n -e FIXME -e TODO myfile.cpp Find lines with `FIXME' that also contain `urgent': $ ugrep -n -e FIXME --and urgent myfile.cpp The same, but with a Boolean query pattern (a space is AND): $ ugrep -n -% 'FIXME urgent' myfile.cpp Find lines with `FIXME' that do not also contain `later': $ ugrep -n -e FIXME --andnot later myfile.cpp The same, but with a Boolean query pattern (a space is AND, - is NOT): $ ugrep -n -% 'FIXME -later' myfile.cpp Output a list of line numbers of lines with `FIXME' but not `later': $ ugrep -e FIXME --andnot later --format='%,%n' myfile.cpp Recursively list all files with both `FIXME' and `LICENSE' anywhere in the file, not necessarily on the same line: $ ugrep -l -%% 'FIXME LICENSE' Find lines with `FIXME' in the C/C++ files stored in a tarball: $ ugrep -z -tc++ -n FIXME project.tgz Recursively find lines with `FIXME' in C/C++ files, but do not search any `bak' and `old' directories: $ ugrep -n FIXME -tc++ -g^bak/,^old/ Recursively search for the word `copyright' in cpio, jar, pax, tar, zip, 7z archives, compressed and regular files, and in PDFs using a PDF filter: $ ugrep -z -w --filter='pdf:pdftotext % -' copyright Match the binary pattern `A3hhhhA3' (hex) in a binary file without Unicode pattern matching -U (which would otherwise match `\xaf' as a Unicode character U+00A3 with UTF-8 byte sequence C2 A3) and display the results in hex with --hexdump with C1 to output one hex line before and after each match: $ ugrep -U --hexdump=C1 '\xa3[\x00-\xff]{2}\xa3' a.out Hexdump an entire file using a pager for viewing: $ ugrep -X --pager '' a.out List all files that are not ignored by one or more `.gitignore': $ ugrep -l '' --ignore-files List all files containing a RPM signature, located in the `rpm' directory and recursively below up to two levels deeper (3 levels total): $ ugrep -3 -l -tRpm '' rpm/ Monitor the system log for bug reports and ungroup multiple matches on a line: $ tail -f /var/log/system.log | ugrep -u -i -w bug Interactive fuzzy search with Boolean search queries: $ ugrep -Q -l -% -Z3 --sort=best Display all words in a MacRoman-encoded file that has CR newlines: $ ugrep --encoding=MACROMAN '\w+' mac.txt Display options related to "fuzzy" searching: $ ugrep --help fuzzyCOPYRIGHT Copyright (c) 2021,2025 Robert A. van Engelen <engelen@acm.org> ugrep is released under the BSD-3 license. All parts of the software have reasonable copyright terms permitting free redistribution. This includes the ability to reuse all or parts of the ugrep source tree.SEE ALSO ugrep-indexer(1), grep(1), zgrep(1).BUGS Report bugs at: <https://github.com/Genivia/ugrep/issues>ugrep 7.2.2 February 3, 2025 UGREP(1)
For PCRE regex patterns with option-P
, please see the PCRE documentationhttps://www.pcre.org/original/doc/html/pcrepattern.html. The pattern syntaxhas more features than the pattern syntax described below. For the patterns incommon the syntax and meaning are the same.
Note that[[:space:]]
and\s
and inverted bracket lists[^...]
aremodified inugrep to prevent matching newlines\n
. This modification isdone to replicate the behavior of grep.
An empty pattern is a special case that matches everything except empty files,i.e. does not match zero-length files, as per POSIX.1 grep standard.
A regex pattern is an extended set of regular expressions (ERE), with nestedsub-expression patternsφ
andψ
:
Pattern | Matches |
---|---|
x | matches the characterx , wherex is not a special character |
. | matches any single character except newline (unless in dotall mode) |
\. | matches. (dot), special characters are escaped with a backslash |
\n | matches a newline, others are\a (BEL),\b (BS),\t (HT),\v (VT),\f (FF), and\r (CR) |
\0 | matches the NUL character |
\cX | matches the control characterX mod 32 (e.g.\cA is\x01 ) |
\0141 | matches an 8-bit character with octal value141 , i.e.a |
\x7f | matches an 8-bit character with hexadecimal value7f |
\x{3B1} | matches Unicode character U+03B1, i.e.α |
\u{3B1} | matches Unicode character U+03B1, i.e.α |
\o{141} | matches Unicode character U+0061, i.e.a , in octal |
\p{C} | matches a character in Unicode category C |
\Q...\E | matches the quoted content between\Q and\E literally |
[abc] | matches one ofa ,b , orc |
[0-9] | matches a digit0 to9 |
[^0-9] | matches any character except a digit and excluding\n |
φ? | matchesφ zero or one time (optional) |
φ* | matchesφ zero or more times (repetition) |
φ+ | matchesφ one or more times (repetition) |
φ{2,5} | matchesφ two to five times (repetition) |
φ{2,} | matchesφ at least two times (repetition) |
φ{2} | matchesφ exactly two times (repetition) |
φ?? | matchesφ zero or once as needed (lazy optional) |
φ*? | matchesφ a minimum number of times as needed (lazy repetition) |
φ+? | matchesφ a minimum number of times at least once as needed (lazy repetition) |
φ{2,5}? | matchesφ two to five times as needed (lazy repetition) |
φ{2,}? | matchesφ at least two times or more as needed (lazy repetition) |
φψ | matchesφ then matchesψ (concatenation) |
φ⎮ψ | matchesφ or matchesψ (alternation) |
(φ) | matchesφ as a group |
(?:φ) | matchesφ as a group without capture |
(?=φ) | matchesφ without consuming it, i.e. lookahead (without option-P : nothing may occur after(?=φ) ) |
(?^φ) | matchesφ and ignores it, marking everything in the pattern as a non-match |
^φ | matchesφ at the start of input or start of a line (nothing may occur before^ ) |
φ$ | matchesφ at the end of input or end of a line (nothing may occur after$ ) |
\Aφ | matchesφ at the start of input (nothing may occur before\A ) |
φ\z | matchesφ at the end of input (nothing may occur after\z ) |
\bφ | matchesφ starting at a word boundary (without option-P : nothing may occur before\b ) |
φ\b | matchesφ ending at a word boundary (without option-P : nothing may occur after\b ) |
\Bφ | matchesφ starting at a non-word boundary (without option-P : nothing may occur before\B ) |
φ\B | matchesφ ending at a non-word boundary (without option-P : nothing may occur after\B ) |
\<φ | matchesφ that starts a word (without option-P : nothing may occur before\< ) |
\>φ | matchesφ that starts a non-word (without option-P : nothing may occur before\> ) |
φ\< | matchesφ that ends a non-word (without option-P : nothing may occur after\< ) |
φ\> | matchesφ that ends a word (without option-P : nothing may occur after\> ) |
(?i:φ) | matchesφ ignoring case |
(?s:φ) | . (dot) inφ matches newline |
(?x:φ) | ignore all whitespace and comments inφ |
(?#:X) | all ofX is skipped as a comment |
The order of precedence for composing larger patterns from sub-patterns is asfollows, from high to low precedence:
- Characters, character classes (bracket expressions), escapes, quotation
- Grouping
(φ)
,(?:φ)
,(?=φ)
, and inline modifiers(?imsux:φ)
- Quantifiers
?
,*
,+
,{n,m}
- Concatenation
φψ
- Anchoring
^
,$
,\<
,\>
,\b
,\B
,\A
,\z
- Alternation
φ|ψ
- Global modifiers
(?imsux)φ
Character classes in bracket lists represent sets of characters. Sets can benegated (inverted), subtracted, intersected, and merged (not supported by PCRE2with option-P
):
Pattern | Matches |
---|---|
[a-zA-Z] | matches a letter |
[^a-zA-Z] | matches a non-letter (character class negation), newlines are not matched |
[a-z−−[aeiou]] | matches a consonant (character class subtraction) |
[a-z&&[^aeiou]] | matches a consonant (character class intersection) |
[a-z⎮⎮[A-Z]] | matches a letter (character class union) |
Bracket lists cannot be empty, so[]
and[^]
are invalid. In fact, thefirst character after the bracket is always part of the list. So[][]
is alist that matches a]
and a[
,[^][]
is a list that matches anything but]
and[
, and[-^]
is a list that matches a-
and a^
.
Negated character classes such as[^a-z]
do not match newlines forcompatibility with traditional grep pattern matching.
The POSIX form can only be used in bracket lists, for example[[:lower:][:digit:]]
matches an ASCII lower case letter or a digit.
You can also use the\p{C}
form for classC
and upper case\P{C}
formthat has the same meaning as\p{^C}
, which matches any character exceptcharacters in the classC
. For example,\P{ASCII}
is the same as\p{^ASCII}
which is the same as[[:^ascii]]
.
POSIX form | Matches |
---|---|
[:ascii:] | matches an ASCII character U+0000 to U+007F including\n |
[:space:] | matches a white space character[ \t\v\f\r] excluding\n |
[:xdigit:] | matches a hex digit[0-9A-Fa-f] |
[:cntrl:] | matches a control character[\x00-\t\x0b-\x1f\x7f] excluding\n |
[:print:] | matches a printable character[\x20-\x7e] |
[:alnum:] | matches a alphanumeric character[0-9A-Za-z] |
[:alpha:] | matches a letter[A-Za-z] |
[:blank:] | matches a blank character\h same as[ \t] |
[:digit:] | matches a digit[0-9] |
[:graph:] | matches a visible character[\x21-\x7e] |
[:lower:] | matches a lower case letter[a-z] |
[:punct:] | matches a punctuation character[\x21-\x2f\x3a-\x40\x5b-\x60\x7b-\x7e] |
[:upper:] | matches an upper case letter[A-Z] |
[:word:] | matches a word character[0-9A-Za-z_] |
[:^blank:] | matches a non-blank characater\H same as[^ \t] |
[:^digit:] | matches a non-digit[^0-9] |
POSIX character categories only cover ASCII,[[:^ascii]]
is empty andtherefore invalid to use. By contrast,[^[:ascii]]
is a Unicode characterclass that excludes the ASCII character category.
Note that the patterns[[:ascii:]]
and negated classes such as[[:^digit:]]
match newlines, which is the official definition of these POSIX categories. Bycontrast, GNU/BSD grep never match newlines. As a consequence, more patternsmay match.
Negated character classes of the form[^...]
match any Unicode characterexcept the given characters and does not match newlines either. For example[^[:digit:]]
matches non-digits (including Unicode) and does not matchnewlines. By contrast,[[:^digit:]]
matches ASCII non-digits, includingnewlines.
Option-U
disables Unicode wide-character matching, i.e. ASCII matching.
Unicode category | Matches |
---|---|
. | matches any single Unicode character except newline\n unless with--dotall |
\a | matches BEL U+0007 |
\d | matches a digit[0-9] or\p{Nd} |
\D | matches a non-digit including\n |
\e | matches ESC U+001b |
\f | matches FF U+000c |
\h | matches a blank[ \t] |
\H | matches a non-blank[^ \t] including\n |
\l | matches a lower case letter\p{Ll} |
\n | matches LF U+000a |
\N | matches a non-LF character |
\r | matches CR U+000d |
\R | matches a Unicode line break (\r\n ,\r ,\v ,\f ,\n , U+0085, U+2028 and U+2029) |
\s | matches a white space character[ \t\v\f\r\x85\p{Z}] excluding\n |
\S | matches a non-white space character and excluding\n |
\t | matches TAB U+0009 |
\u | matches an upper case letter\p{Lu} |
\v | matches VT U+000b or vertical space character with option-P |
\w | matches a word character[0-9A-Za-z_] or[\p{L}\p{Nd}\p{Pc}] |
\W | matches a non-Unicode word character including\n |
\X | matches any ISO-8859-1 or Unicode character including\n |
\p{Space} | matches a white space character[ \t\v\f\r\x85\p{Z}] excluding\n |
\p{Unicode} | matches any Unicode character U+0000 to U+10FFFF minus U+D800 to U+DFFF |
\p{ASCII} | matches an ASCII character U+0000 to U+007F including\n |
\p{Non_ASCII_Unicode} | matches a non-ASCII character U+0080 to U+10FFFF minus U+D800 to U+DFFF |
\p{L&} | matches a character with Unicode property L& (i.e. property Ll, Lu, or Lt) |
\p{Letter} ,\p{L} | matches a character with Unicode property Letter |
\p{Mark} ,\p{M} | matches a character with Unicode property Mark |
\p{Separator} ,\p{Z} | matches a character with Unicode property Separator |
\p{Symbol} ,\p{S} | matches a character with Unicode property Symbol |
\p{Number} ,\p{N} | matches a character with Unicode property Number |
\p{Punctuation} ,\p{P} | matches a character with Unicode property Punctuation |
\p{Other} ,\p{C} | matches a character with Unicode property Other |
\p{Lowercase_Letter} ,\p{Ll} | matches a character with Unicode sub-property Ll |
\p{Uppercase_Letter} ,\p{Lu} | matches a character with Unicode sub-property Lu |
\p{Titlecase_Letter} ,\p{Lt} | matches a character with Unicode sub-property Lt |
\p{Modifier_Letter} ,\p{Lm} | matches a character with Unicode sub-property Lm |
\p{Other_Letter} ,\p{Lo} | matches a character with Unicode sub-property Lo |
\p{Non_Spacing_Mark} ,\p{Mn} | matches a character with Unicode sub-property Mn |
\p{Spacing_Combining_Mark} ,\p{Mc} | matches a character with Unicode sub-property Mc |
\p{Enclosing_Mark} ,\p{Me} | matches a character with Unicode sub-property Me |
\p{Space_Separator} ,\p{Zs} | matches a character with Unicode sub-property Zs |
\p{Line_Separator} ,\p{Zl} | matches a character with Unicode sub-property Zl |
\p{Paragraph_Separator} ,\p{Zp} | matches a character with Unicode sub-property Zp |
\p{Math_Symbol} ,\p{Sm} | matches a character with Unicode sub-property Sm |
\p{Currency_Symbol} ,\p{Sc} | matches a character with Unicode sub-property Sc |
\p{Modifier_Symbol} ,\p{Sk} | matches a character with Unicode sub-property Sk |
\p{Other_Symbol} ,\p{So} | matches a character with Unicode sub-property So |
\p{Decimal_Digit_Number} ,\p{Nd} | matches a character with Unicode sub-property Nd |
\p{Letter_Number} ,\p{Nl} | matches a character with Unicode sub-property Nl |
\p{Other_Number} ,\p{No} | matches a character with Unicode sub-property No |
\p{Dash_Punctuation} ,\p{Pd} | matches a character with Unicode sub-property Pd |
\p{Open_Punctuation} ,\p{Ps} | matches a character with Unicode sub-property Ps |
\p{Close_Punctuation} ,\p{Pe} | matches a character with Unicode sub-property Pe |
\p{Initial_Punctuation} ,\p{Pi} | matches a character with Unicode sub-property Pi |
\p{Final_Punctuation} ,\p{Pf} | matches a character with Unicode sub-property Pf |
\p{Connector_Punctuation} ,\p{Pc} | matches a character with Unicode sub-property Pc |
\p{Other_Punctuation} ,\p{Po} | matches a character with Unicode sub-property Po |
\p{Control} ,\p{Cc} | matches a character with Unicode sub-property Cc |
\p{Format} ,\p{Cf} | matches a character with Unicode sub-property Cf |
\p{UnicodeIdentifierStart} | matches a character in the Unicode IdentifierStart class |
\p{UnicodeIdentifierPart} | matches a character in the Unicode IdentifierPart class |
\p{IdentifierIgnorable} | matches a character in the IdentifierIgnorable class |
\p{JavaIdentifierStart} | matches a character in the Java IdentifierStart class |
\p{JavaIdentifierPart} | matches a character in the Java IdentifierPart class |
\p{CsIdentifierStart} | matches a character in the C# IdentifierStart class |
\p{CsIdentifierPart} | matches a character in the C# IdentifierPart class |
\p{PythonIdentifierStart} | matches a character in the Python IdentifierStart class |
\p{PythonIdentifierPart} | matches a character in the Python IdentifierPart class |
To specify a Unicode block as a category use\p{IsBlockName}
with a UnicodeBlockName
.
To specify a Unicode language script, use\p{Language}
with a UnicodeLanguage
.
Unicode language script character classes differ from the Unicode blocks thathave a similar name. For example, the\p{Greek}
class represents Greek andCoptic letters and differs from the Unicode block\p{IsGreek}
that spans aspecific Unicode block of Greek and Coptic characters only, which also includesunassigned characters.
For the pattern syntax ofugrep option-P
(Perl regular expressions), seefor examplePerl regular expression syntax.However,ugrep enhances the Perl regular expression syntax with all of thefeatures listed inPOSIX regular expression syntax.
If something is not working, then please check thetutorial andtheman page. If you can't find it there and it looks like a bug, thenreport an issue on GitHub. Bugreports are quickly addressed.
Copyright (c) Robert van Engelen, 2025
About
🔍 ugrep 7.3 file pattern searcher -- a more powerful, ultra fast, user-friendly, compatible grep replacement. Includes a TUI, Google-like Boolean search with AND/OR/NOT, fuzzy search, hexdumps, searches (nested) archives (zip, 7z, tar, pax, cpio), compressed files (gz, Z, bz2, lzma, xz, lz4, zstd, brotli), pdfs, docs, and more