A fast and simple implementation of the cut command in Rust. Extract specific columns from delimited text files or stdin.
- Extract specific columns from delimited text files
- Support for tab, comma, and custom delimiters
- Read from files or stdin
- Flexible field specification (comma or space-separated)
- Clear error messages for invalid input
- Zero external dependencies
git clone git@github.com:MichaelKlank/cut-rs.git
cd cut-rs
cargo build --releaseThe binary will be located at target/release/cut-rs.
cargo install --path .cut-rs [FILE] -f <fields> [-d <delimiter>]-f, --fields <fields>- Required. Comma or space-separated list of field indices to extract (0-based)-d, --delimiter <delimiter>- Delimiter to use (default: tab character\t)-h, --help- Show help message
Extract columns 1 and 2 from a tab-separated file:
cut-rs sample.tsv -f 1,2All of these are equivalent:
# Comma-separated
cut-rs file.txt -f 1,2,3
# Space-separated (with quotes)
cut-rs file.txt -f "1 2 3"
# Compact format
cut-rs file.txt -f1,2,3
# Long form
cut-rs file.txt --fields 1,2,3Extract columns from a comma-separated file:
cut-rs data.csv -f 0,2,4 -d ,Extract columns from a pipe-separated file:
cut-rs data.txt -f 1,3 -d "|"echo -e "a\tb\tc\nd\te\tf" | cut-rs -f 0,2Or with a custom delimiter:
echo "a,b,c" | cut-rs -f 0,2 -d ,The input file can be specified before or after options:
cut-rs file.txt -f 1,2
cut-rs -f 1,2 file.txt
cut-rs -f 1,2 -d , file.txtField indices are 0-based, meaning:
0refers to the first column1refers to the second column- And so on...
The program provides clear error messages for common issues:
$ cut-rs file.txt
Error: -f (fields) is required$ cut-rs file.txt -f 1,5 -d ,
Error: field index 5 is out of bounds (line 1, 3 fields found, delimiter: ",")If you specify the wrong delimiter, you'll get an out-of-bounds error indicating how many fields were actually found:
$ cut-rs sample.tsv -f 1,2 -d ,
Error: field index 1 is out of bounds (line 1, 1 fields found, delimiter: ",")This helps you identify that the delimiter is incorrect (the file has tab-separated values, but you specified comma).
This implementation is similar to the Unix cut command but with some differences:
- 0-based indexing (standard
cutuses 1-based) - Simpler syntax for field specification
- Better error messages with context about what went wrong
- No external dependencies (pure Rust)
# Debug build
cargo build
# Release build
cargo build --release
# Run tests (if any)
cargo test
# Run directly
cargo run -- -f 1,2 sample.tsv[Add your license here]
[Add contribution guidelines here]