← 返回命令列表

Linux command

csvgrep 命令

文本

复制后可按需替换文件名、目录或参数。

常用示例

Filter rows by column

csvgrep -c [column] -m "[value]" [data.csv]

Filter using regular expression

csvgrep -c [column] -r "[pattern]" [data.csv]

Filter by column number

csvgrep -c [1] -m "[value]" [data.csv]

Invert match

csvgrep -c [column] -m "[value]" -i [data.csv]

Match against multiple columns

csvgrep -c "[col1,col2]" -m "[value]" [data.csv]

Case-insensitive matching

csvgrep -c [column] -r "(?i)[pattern]" [data.csv]

说明

csvgrep is part of csvkit that filters CSV rows based on column values. Unlike standard grep, it understands CSV structure and can match against specific columns while preserving proper CSV output. The tool supports both exact string matching and regular expressions. Multiple columns can be searched, and the match sense can be inverted to exclude rows. This makes it ideal for extracting subsets of data based on criteria. csvgrep handles quoted fields correctly, ensuring that commas within fields don't break the search. Results include the header row, maintaining valid CSV structure.

参数

-c _COLUMN_, --columns _COLUMN_
Column name(s) or index to search (comma-separated for multiple).
-m _STRING_, --match _STRING_
Exact string to search for.
-r _REGEX_, --regex _REGEX_
Regular expression pattern to match.
-f _FILE_, --file _FILE_
File containing values to match (one per line).
-i, --invert-match
Select non-matching rows.
-a, --any-match
Match if any column matches (with multiple columns).
-d _CHAR_, --delimiter _CHAR_
Field delimiter (default: comma).
-e _ENCODING_, --encoding _ENCODING_
Input file encoding.

FAQ

What is the csvgrep command used for?

csvgrep is part of csvkit that filters CSV rows based on column values. Unlike standard grep, it understands CSV structure and can match against specific columns while preserving proper CSV output. The tool supports both exact string matching and regular expressions. Multiple columns can be searched, and the match sense can be inverted to exclude rows. This makes it ideal for extracting subsets of data based on criteria. csvgrep handles quoted fields correctly, ensuring that commas within fields don't break the search. Results include the header row, maintaining valid CSV structure.

How do I run a basic csvgrep example?

Run `csvgrep -c [column] -m "[value]" [data.csv]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does -c _COLUMN_, --columns _COLUMN_ do in csvgrep?

Column name(s) or index to search (comma-separated for multiple).