Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cut-rs

A fast and simple implementation of the cut command in Rust. Extract specific columns from delimited text files or stdin.

Features

  • 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

Installation

From Source

git clone git@github.com:MichaelKlank/cut-rs.git
cd cut-rs
cargo build --release

The binary will be located at target/release/cut-rs.

Using Cargo

cargo install --path .

Usage

cut-rs [FILE] -f <fields> [-d <delimiter>]

Options

  • -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

Examples

Basic Usage

Extract columns 1 and 2 from a tab-separated file:

cut-rs sample.tsv -f 1,2

Different Field Formats

All 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,3

Custom Delimiters

Extract 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 "|"

Reading from stdin

echo -e "a\tb\tc\nd\te\tf" | cut-rs -f 0,2

Or with a custom delimiter:

echo "a,b,c" | cut-rs -f 0,2 -d ,

File Order

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.txt

Field Indices

Field indices are 0-based, meaning:

  • 0 refers to the first column
  • 1 refers to the second column
  • And so on...

Error Handling

The program provides clear error messages for common issues:

Missing Required Option

$ cut-rs file.txt
Error: -f (fields) is required

Invalid Field Index

$ cut-rs file.txt -f 1,5 -d ,
Error: field index 5 is out of bounds (line 1, 3 fields found, delimiter: ",")

Wrong 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).

Comparison with Standard cut

This implementation is similar to the Unix cut command but with some differences:

  • 0-based indexing (standard cut uses 1-based)
  • Simpler syntax for field specification
  • Better error messages with context about what went wrong
  • No external dependencies (pure Rust)

Building

# Debug build
cargo build

# Release build
cargo build --release

# Run tests (if any)
cargo test

# Run directly
cargo run -- -f 1,2 sample.tsv

License

[Add your license here]

Contributing

[Add contribution guidelines here]

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages