はじめに

CLIでCSVを閲覧できる csvlensというコマンドがあるということで使用してみる。
CLIでCSVを閲覧できるcsviewを使ってみるでは csviewを使ってCLIでCSVを閲覧したが今回はcsvlensというツールを使う。

環境

1
2
MacOS Sonoma 14.4.1
Homebrew 4.2.15

csvlensのインストール

1
brew install csvlens
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
brew install csvlens
==> Auto-updating Homebrew...
Adjust how often this is run with HOMEBREW_AUTO_UPDATE_SECS or disable with
HOMEBREW_NO_AUTO_UPDATE. Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
==> Auto-updated Homebrew!
Updated 2 taps (homebrew/cask-versions and homebrew/core).
==> New Formulae
dissent

You have 1 outdated formula installed.

==> Downloading https://ghcr.io/v2/homebrew/core/csvlens/manifests/0.8.1
################################################################################################################################################# 100.0%
==> Fetching csvlens
==> Downloading https://ghcr.io/v2/homebrew/core/csvlens/blobs/sha256:7223393a61d740183f34a55e74d672a68625f1e6899ea9d9d61c8ae6adfdaf8b
################################################################################################################################################# 100.0%
==> Pouring csvlens--0.8.1.sonoma.bottle.tar.gz
🍺  /usr/local/Cellar/csvlens/0.8.1: 8 files, 6.7MB
==> Running `brew cleanup csvlens`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).

使ってみる

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
csvlens --help

Usage: csvlens [OPTIONS] [FILENAME]

Arguments:
  [FILENAME]  CSV filename

Options:
  -d, --delimiter <char>           Delimiter character (comma by default) or "auto" to auto-detect the delimiter
  -t, --tab-separated              Use tab separation. Shortcut for -d '\t'
      --no-headers                 Do not interpret the first row as headers
      --columns <regex>            Use this regex to select columns to display by default
      --filter <regex>             Use this regex to filter rows to display by default
      --find <regex>               Use this regex to find and hightlight matches by default
  -i, --ignore-case                Searches ignore case. Ignored if any uppercase letters are present in the search string
      --echo-column <column_name>  Print the value of this column to stdout for the selected row
      --debug                      Show stats for debugging
  -h, --help                       Print help
  -V, --version                    Print version

CSVファイルを適当に用意する
今回は下記からダウンロードする。

1
$ csvlens 
1
2
3
4
5
6
csvlens c01.csv

Failed to open file: c01.csv

Caused by:
    CSV parse error: record 0 (line 1, field: 0, byte: 0): invalid utf-8: invalid UTF-8 in field 0 near byte index 0

読めないみたいだった。

UTF-8でないと読み込めないみたい。
まずは文字コードを確認してみる。

1
2
~/Downloads$nkf --guess c01.csv 
Shift_JIS (CRLF)

Shift_JISだった。
UTF-8に変換する。

1
$ nkf -w c01.csv > c01_utf-8.csv

再度csvlens でみてみる。

1
csvlens c01_utf-8.csv

かなり綺麗に見れるなー。

おわりに

csviewとは違って 1画面ずつ表示されるので便利。
大きいファイルでも行移動などができるので良いな〜。