Linux command
csvq 命令
文本
复制后可按需替换文件名、目录或参数。
常用示例
Query a CSV file with SQL
csvq "SELECT * FROM [data.csv]"
Filter rows with WHERE
csvq "SELECT * FROM [data.csv] WHERE [column] = '[value]'"
Aggregate data
csvq "SELECT [category], COUNT(*) FROM [data.csv] GROUP BY [category]"
Join multiple CSV files
csvq "SELECT * FROM [a.csv] JOIN [b.csv] ON [a.id] = [b.id]"
Sort results
csvq "SELECT * FROM [data.csv] ORDER BY [column] DESC"
Output as JSON
csvq -f json "SELECT * FROM [data.csv]"
Read from stdin via pipe
cat [data.csv] | csvq "SELECT * FROM STDIN"
Start interactive mode
csvq
说明
csvq is a command-line tool that enables SQL queries against CSV files. It treats CSV files as database tables, supporting SELECT, INSERT, UPDATE, DELETE, and complex queries with JOINs, subqueries, and aggregate functions. Column names come from the CSV header row (or can be specified for headerless files). Standard SQL syntax is supported including WHERE, GROUP BY, HAVING, ORDER BY, LIMIT, UNION, and window functions. Multiple CSV files can be queried together with JOINs. Results can be output in various formats including CSV, JSON, and formatted tables, making it useful for data transformation pipelines.
参数
- -f, --format _format_
- Output format: CSV, TSV, FIXED, JSON, JSONL, LTSV, GFM, ORG, BOX, TEXT (default: TEXT).
- -d, --delimiter _char_
- Field delimiter character (default: comma).
- -o, --out _file_
- Output to file instead of stdout.
- -e, --encoding _encoding_
- Input file encoding (AUTO, UTF8, UTF8M, UTF16, SJIS, etc.).
- -n, --no-header
- Treat first line as data; fields auto-named c1, c2, etc.
- -N, --without-header
- Export results without header row.
- -l, --line-break _type_
- Output line break type: CRLF, CR, LF (default: LF).
- -E, --write-encoding _encoding_
- Output file encoding.
- -s, --source _file_
- Load queries from a file.
- -r, --repository _path_
- Directory containing data files (default: current directory).
- -q, --quiet
- Suppress operation log messages.
- -P, --pretty-print
- Pretty-print JSON output.
FAQ
What is the csvq command used for?
csvq is a command-line tool that enables SQL queries against CSV files. It treats CSV files as database tables, supporting SELECT, INSERT, UPDATE, DELETE, and complex queries with JOINs, subqueries, and aggregate functions. Column names come from the CSV header row (or can be specified for headerless files). Standard SQL syntax is supported including WHERE, GROUP BY, HAVING, ORDER BY, LIMIT, UNION, and window functions. Multiple CSV files can be queried together with JOINs. Results can be output in various formats including CSV, JSON, and formatted tables, making it useful for data transformation pipelines.
How do I run a basic csvq example?
Run `csvq "SELECT * FROM [data.csv]"` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does -f, --format _format_ do in csvq?
Output format: CSV, TSV, FIXED, JSON, JSONL, LTSV, GFM, ORG, BOX, TEXT (default: TEXT).