replace

May 31, 2026 · View on GitHub

Replace CSV data using a regex. Applies the regex to each field individually.

Table of Contents | Source: src/cmd/replace.rs | 📇🏎️👆

Description | Examples | Usage | Arguments | Replace Options | Common Options

Description ↩

Replace occurrences of a pattern across a CSV file.

You can of course match groups using parentheses and use those in the replacement string. But don't forget to escape your $ in bash by using a backslash or by wrapping the replacement string into single quotes:

$ qsv replace 'hel(lo)' 'hal\$1' file.csv
$ qsv replace "hel(lo)" "hal\$1" file.csv

Returns exitcode 0 when replacements are done, returning number of replacements to stderr. Returns exitcode 1 when no replacements are done, unless the '--not-one' flag is used.

When the CSV is indexed, a faster parallel replace is used. If there were any replacements, the index will be refreshed.

Examples ↩

Replace all occurrences of 'hello' with 'world' in the file.csv file.

qsv replace 'hello' 'world' file.csv

Replace all occurrences of 'hello' with 'world' in the file.csv file and save the output to the file.out file.

qsv replace 'hello' 'world' file.csv -o file.out

Replace all occurrences of 'hello' case insensitive with 'world' in the file.csv file.

qsv replace 'hello' 'world' file.csv -i

Replace all valid email addresses (using a regex) with '' in the file.csv file.

qsv replace '([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})' \
'<EMAIL>' file.csv

For more examples, see tests.

See also https://github.com/dathere/qsv/wiki/Transform-and-Reshape#replace

Usage ↩

qsv replace [options] <pattern> <replacement> [<input>]
qsv replace --help

Arguments ↩

   Argument    Description
 <pattern> Regular expression pattern to match. Uses Rust regex syntax. See https://docs.rs/regex/latest/regex/index.html#syntax or https://regex101.com with the Rust flavor for more info.
 <input> The CSV file to read. If not given, reads from stdin.
 <replacement> Replacement string. Set to '' if you want to replace matches with ''.

Replace Options ↩

      Option      TypeDescriptionDefault
 ‑i,
‑‑ignore‑case 
flagCase insensitive search. This is equivalent to prefixing the regex with '(?i)'.
 ‑‑literal flagTreat the regex pattern as a literal string. This allows you to search for matches that contain regex special characters.
 ‑‑exact flagMatch the ENTIRE field exactly. Treats the pattern as a literal string (like --literal) and automatically anchors it to match the complete field value (^pattern$).
 ‑s,
‑‑select 
stringSelect the columns to search. See 'qsv select -h' for the full syntax.
 ‑u,
‑‑unicode 
flagEnable unicode support. When enabled, character classes will match all unicode word characters instead of only ASCII word characters. Decreases performance.
 ‑‑size‑limit integerSet the approximate size limit (MB) of the compiled regular expression. If the compiled expression exceeds this number, then a compilation error is returned.50
 ‑‑dfa‑size‑limit integerSet the approximate size of the cache (MB) used by the regular expression engine's Discrete Finite Automata.10
 ‑‑not‑one flagUse exit code 0 instead of 1 for no replacement found.
 ‑j,
‑‑jobs 
integerThe number of jobs to run in parallel when the given CSV data has an index. Note that a file handle is opened for each job. When not set, defaults to the number of CPUs detected.

Common Options ↩

     Option     TypeDescriptionDefault
 ‑h,
‑‑help 
flagDisplay this message
 ‑o,
‑‑output 
stringWrite output to instead of stdout.
 ‑n,
‑‑no‑headers 
flagWhen set, the first row will not be interpreted as headers. (i.e., They are not searched, analyzed, sliced, etc.)
 ‑d,
‑‑delimiter 
stringThe field delimiter for reading CSV data. Must be a single character. (default: ,)
 ‑p,
‑‑progressbar 
flagShow progress bars. Not valid for stdin.
 ‑q,
‑‑quiet 
flagDo not print number of replacements to stderr.

Source: src/cmd/replace.rs | Table of Contents | README