1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ログ解析Tips: cut や awk 代替の有力候補 『xcut』 で簡単抽出

Posted at

🔧 背景

日常的にログファイルを解析したり、CI/CD のビルドログを読み込んだりする場面は多いと思います。ここで、Unix系の発想力の高いフィルタコマンドの代表格「cut」や「awk」が注目されるわけですが、これらに比べて、もっと直感的でわかりやすいコマンドが欲しいと思ったことはありませんか?
そこで、もっと直感的にログ抽出ができるCLIツールをRustで作りました。

xcut とは?

Rustで開発された、以下のような特徴を持つコマンドです:

  • --cols で指定した列だけを抽出
  • --filter で情報を行単位で濾り返し
  • --delim / --max-split で枠切り解析に対応
  • 次のユースケースでも役立ちます

💡 ユースケース:「エラーの内容と発生時刻だけを取り出したい」

🔍 例:system.log

2024-06-01 10:00:00 INFO    Service started
2024-06-01 10:05:12 ERROR   Connection timeout: DB
2024-06-01 10:06:30 WARN    Retry requested
2024-06-01 10:07:01 ERROR   Disk full on /var

xcut での実行

xcut --input system.log \
     --filter 'col(3) == "ERROR"' \
     --cols 1,2,4 \
     --delim ' ' --max-split 4

✅ 出力

2024-06-01 10:05:12 Connection timeout: DB
2024-06-01 10:07:01 Disk full on /var

🤔 cut / awkとの比較

ツール 難点 xcut の強み
cut --max-splitがない。条件フィルタ不可 --filter, --max-split
awk 構文が複雑で習得が難しい col(N) == "val" の直感構文
grep フィールド抽出不可 --cols で任意フィールド取得

🧠 応用:パターンマッチ

xcut --input system.log \
     --filter 'col(4) =~ "timeout|full"' \
     --cols 1,2,4 \
     --delim ' ' --max-split 4
2024-06-01 10:05:12 Connection timeout: DB
2024-06-01 10:07:01 Disk full on /var

🏁 まとめ

xcut は、ユーザーにやさしく、フィルタをわかりやすい形式で指定できるのが役立ちます。「データログの抽出」、「特定文字列を含む行の探索」など、grep / cut / awk を使い切れない日常の作業に役立つ手頓となるはずです。


📎 リンク

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?