mirror of
https://gitea.com/mcereda/oam.git
synced 2026-02-08 21:34:25 +00:00
3.1 KiB
3.1 KiB
Grep
TL;DR
# Basic search.
grep 'pattern' 'path/to/search'
# Search recursively.
grep -R 'pattern' 'path/to/search/recursively'
grep -R --exclude-dir 'excluded/dir' 'pattern' 'path/to/search/recursively' # gnu grep >= 2.5.2
# Show line numbers.
grep -n 'pattern' 'path/to/search'
# Only print the part matching the pattern.
ps | grep -o '/.*/fish' | head -n '1'
# Multiple parallel searches.
# Mind files with spaces in their name.
find . -type f | parallel -j +100% grep 'pattern'
find . -type f -print0 | xargs -0 -n 1 -P "$(nproc)" grep 'pattern'
# Highlight numbers in strings.
grep --color '[[:digit:]]' 'file.txt'
# Only print text after a delimiter.
echo "string,with,delimiters" | grep -o '[^,]*$'
echo "string/with/delimiters" | grep -o '[^/]*$'
Variants
egrepto use regular expressions in search patterns, same asgrep -Efgrep] to use patterns as fixed strings, same asgrep -F- archive-related variants for searching into compressed files
pdfgrepfor searching into PDF files
Archive-related variants
xzgrep(withxzegrepandxzfgrep)zstdgrepfor zstd archives- many many others
PDFgrep
For simple searches, you might want to use pdfgrep.
Should you need more advanced grep capabilities not incorporated by pdfgrep, you might want to convert the file to text
and search there.
You can to this using pdftotext as shown in this example
(source):
find /path -name '*.pdf' -exec sh -c 'pdftotext "{}" - | grep --with-filename --label="{}" --color "your pattern"' ';'
Gotchas
-
Standard editions of
greprun in a single thread; use another executor likeparallelorxargsto parallelize grepping multiple files:find . -type f | parallel -j 100% grep 'pattern' find . -type f -print0 | xargs -0 -n 1 -P $(nproc) grep 'pattern'mind files with spaces in their name.