2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Rustでcut/awk/grepを再構築:「xcut」というCLIツールを作った話

Last updated at Posted at 2025-06-03

はじめに

日常的にログやCSVのテキスト加工を行う中で、「cut」や「awk」「grep」を組み合わせるのが面倒だなと感じることが増えてきました。

  • cutは柔軟性に欠ける
  • awkは表現力は高いけど記述が複雑
  • PowerShellでは思った通り動かない

そこで、Rustで「cutの拡張版」を自作しました。名前は xcut です。


xcutとは?

Rust製のCLIツールで、以下の特徴があります:

  • 列(カラム)の抽出が簡単
  • 正規表現や論理式によるフィルタが書ける
  • --head / --tail で行数制限できる
  • 区切り文字を柔軟に指定できる
  • 標準入力/標準出力に対応、PowerShellでも動く

インストール(開発版)

1. GitHub Releases からダウンロード

  1. 以下のリリースページを開きます:

    👉 https://github.com/kyotalab/xcut/releases

  2. ご利用の OS に合わせてバイナリをダウンロードします(例:xcut

  3. ダウンロードしたバイナリに実行権限を与えます:

    chmod +x xcut
    
  4. 任意のパスに移動(例:/usr/local/bin):

    sudo mv xcut /usr/local/bin/
    
  5. 正しくインストールされたか確認します:

    xcut --version
    

2. Homebrew でインストール(macOS / Linux)

Homebrew を使ってインストールすることも可能です。

brew tap kyotalab/xcut
brew install xcut

更新時は以下でアップグレードできます:

brew upgrade xcut

🧪 動作確認

以下のように実行して、ヘルプが表示されれば成功です:

xcut --help

使用例

1. 列の抽出

xcut --input file.txt --cols 1,3

2. フィルタ条件(正規表現 + 論理式)

xcut --filter 'col(3) == "INFO" && col(4) =~ "CPU"'

3. 区切り文字や最大分割数を指定して柔軟に加工

xcut --delim ',' --max-split 4 --cols 1,2

4. 最初と最後だけ抽出(head/tail)

xcut --head 5
xcut --tail 10

5. 出力をファイルに保存

xcut --output filtered.log

内部構成と工夫した点

  • clap を使ったわかりやすいCLI引数設計
  • regex + evalexpr による柔軟な条件式評価
  • VecDeque を使ってtailを効率的に処理
  • anyhow + context() で文脈のあるエラー処理
  • assert_cmd でCLIテストも完備!

ソースコード

GitHubはこちらです。Star歓迎です!


まとめ

CLIツールに少しでも触れている人には「cutの上位互換」として便利だと思います。

今後は以下も実装予定です:

  • Windows PowerShellでの互換性強化
  • --replace--sort など加工系の追加
  • crates.ioへの登録(cargo install xcut

おわりに

「cutでは物足りない」そんな方はぜひ試してみてください!

QiitaのコメントやGitHub Issueでのフィードバックもお待ちしています 🙏

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?