Replace, Preprocess, and Encodings

Preview replacements, search transformed files, and handle compressed or non-UTF8 data.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all

Replacement previews

Use ripgrep to preview transformed output before applying changes with another tool.

Preview a simple replacement

Show what matched lines would look like after substitution.

bashANYripgrepreplace
bash
rg --replace 'logger.error($1)' 'console\.error\((.*)\)' src/

This is a preview only; combine with `sd`, `perl`, or editor tools for actual rewrites.

Use capture groups in replacements

Reuse matched text in the replacement output.

bashANYripgrepreplacecapture-group
bash
rg --replace '$2_$1' '(\w+)-(\w+)' sample.txt

Capture-group replacement is great for exploring renames before making edits.

Print replaced fragments only

Combine `-o` and `--replace` to extract transformed matches.

bashANYripgrepreplaceextract
bash
rg -o --replace '$1' 'name="([^"]+)"' template.html

Useful for harvesting identifiers or values from structured text.

Preview advanced regex replacements

Use PCRE2 pattern features with replacement preview.

bashANYripgreppcre2replace
bash
rg -P --replace '$1' '(?<=id=)\d+' data.txt

This works well when look-around is needed to isolate the match.

Preview and select candidates interactively

Pipe results into a fuzzy finder before editing.

bashANYripgrepfzfworkflow
bash
rg --line-number --no-heading 'TODO' . | fzf

ripgrep plus fzf is a classic workflow for interactive navigation and cleanup.

Preprocessors and compressed files

Search data that needs to be transformed before matching.

Search compressed files

Enable searching in common compressed formats.

bashANYripgrepcompressed
bash
rg -z 'Exception' logs/

`-z` lets ripgrep inspect gzip, xz, bzip2, lz4, brotli, and similar compressed inputs when supported.

Use a preprocessor for unsupported formats

Transform file content before ripgrep searches it.

bashANYripgreppreprocessorpdf
bash
rg --pre 'pdftotext -layout' 'invoice' docs/*.pdf

Preprocessors are powerful when the raw file format is not plain text.

Apply preprocessing only to certain files

Limit preprocessors to matching paths.

bashANYripgreppre-glob
bash
rg --pre 'pandoc -t plain' --pre-glob '*.docx' 'roadmap' docs/

`--pre-glob` avoids paying a preprocessing cost for unrelated files.

Combine archive tools with rg

Expand an archive stream and search it.

bashANYripgreparchivespipeline
bash
tar -xOf backup.tar config/app.yml | rg 'database:'

Sometimes a pipeline is simpler than a custom preprocessor.

Search binary-ish files as text

Treat binary files as text input.

bashANYripgrepbinarytext
bash
rg -a 'sqlite' mystery.bin

`-a` can be useful for generated files, dumps, or partially text-based formats.

Encodings and text quirks

Handle non-UTF8 files, BOMs, and line-ending differences.

Search a UTF-16LE file

Decode a file using a specific encoding.

bashANYripgrepencodingutf16
bash
rg --encoding utf-16le 'Version' report.txt

This is common with Windows exports and some vendor logs.

Search Shift_JIS text

Decode legacy encoded content before matching.

bashANYripgrepencodingshift-jis
bash
rg --encoding shift_jis 'エラー' app.log

Encoding support helps when working across older systems or regional datasets.

Disable line numbers for transformed output

Print cleaner replacement previews.

bashANYripgrepreplacepreview
bash
rg -N --replace '$1' '"([^"]+)"' data.json

`-N` is handy when line numbers would clutter preview output.

Print NUL-delimited paths

Use NUL separators to make filename parsing safe.

bashANYripgrepnulxargs
bash
rg -l -0 'TODO' . | xargs -0 sed -n '1p'

This is the shell-safe way to handle paths with spaces or newlines.

Recommended next

No recommendations yet.