はじめに

CLIでCSVを閲覧できる csviewというコマンドがあるということで使用してみる。

環境

1
2
MacOS Sonoma 14.0
Homebrew 4.1.19

csviewのインストール

1
$ brew install csview
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
==> Downloading https://ghcr.io/v2/homebrew/core/csview/manifests/1.2.2
################################################################################################################################ 100.0%
==> Fetching csview
==> Downloading https://ghcr.io/v2/homebrew/core/csview/blobs/sha256:ef5fc823eba3c9ebf5d8ee9390854bf155e1c4444a1bdf14a6a66b5b0fd807b2
################################################################################################################################ 100.0%
==> Pouring csview--1.2.2.sonoma.bottle.tar.gz
==> Caveats
zsh completions have been installed to:
  /usr/local/share/zsh/site-functions
==> Summary
🍺  /usr/local/Cellar/csview/1.2.2: 11 files, 979.7KB
==> Running `brew cleanup csview`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).

使ってみる

1
csview --help
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
A high performance csv viewer with cjk/emoji support.

Usage: csview [OPTIONS] [FILE]

Arguments:
  [FILE]
          File to view

Options:
  -H, --no-headers
          Specify that the input has no header row
  -n, --number
          Prepend a column of line numbers to the table
  -t, --tsv
          Use '\t' as delimiter for tsv
  -d, --delimiter <DELIMITER>
          Specify the field delimiter [default: ,]
  -s, --style <STYLE>
          Specify the border style [default: sharp] [possible values: none, ascii, ascii2, sharp, rounded, reinforced, markdown, grid]
  -p, --padding <PADDING>
          Specify padding for table cell [default: 1]
  -i, --indent <INDENT>
          Specify global indent for table [default: 0]
      --sniff <LIMIT>
          Limit column widths sniffing to the specified number of rows. Specify "0" to cancel limit [default: 1000]
      --header-align <HEADER_ALIGN>
          Specify the alignment of the table header [default: center] [possible values: left, center, right]
      --body-align <BODY_ALIGN>
          Specify the alignment of the table body [default: left] [possible values: left, center, right]
  -h, --help
          Print help information
  -V, --version
          Print version information

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

1
$ csview 
1
2
csview c01.csv 
csview: 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
$ nkf --guess c01.csv 
Shift_JIS (CRLF)

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

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

再度csview でみてみる。

1
csview c01_utf-8.csv

またエラー

1
2
$ csview c01_utf-8.csv
csview: CSV error: record 981 (line: 981, byte: 59926): found record with 1 fields, but the previous record has 9 fields

中身を見てみると、最後の2行のレコードのデータ数がおかしいみたい。
この2行を削って再度実施してみる。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
~/Downloads$csview c01_utf-8.csv
┌────────────────┬────────────────────────┬──────┬────────────┬────────────┬────┬──────────────┬────────────┬────────────┐
│ 都道府県コード │       都道府県名       │ 元号 │ 和暦(年) │ 西暦(年) │ 注 │ 人口(総数) │ 人口(男) │ 人口(女) │
├────────────────┼────────────────────────┼──────┼────────────┼────────────┼────┼──────────────┼────────────┼────────────┤
│ 00             │ 全国                   │ 大正 │ 9          │ 1920       │    │ 55963053     │ 28044185   │ 27918868   │
│ 01             │ 北海道                 │ 大正 │ 9          │ 1920       │    │ 2359183      │ 1244322    │ 1114861    │
│ 02             │ 青森県                 │ 大正 │ 9          │ 1920       │    │ 756454       │ 381293     │ 375161     │
│ 03             │ 岩手県                 │ 大正 │ 9          │ 1920       │    │ 845540       │ 421069     │ 424471     │
│ 04             │ 宮城県                 │ 大正 │ 9          │ 1920       │    │ 961768       │ 485309     │ 476459     │
│ 05             │ 秋田県                 │ 大正 │ 9          │ 1920       │    │ 898537       │ 453682     │ 444855     │
│ 06             │ 山形県                 │ 大正 │ 9          │ 1920       │    │ 968925       │ 478328     │ 490597     │
│ 07             │ 福島県                 │ 大正 │ 9          │ 1920       │    │ 1362750      │ 673525     │ 689225     │
│ 08             │ 茨城県                 │ 大正 │ 9          │ 1920       │    │ 1350400      │ 662128     │ 688272     │
│ 09             │ 栃木県                 │ 大正 │ 9          │ 1920       │    │ 1046479      │ 514255     │ 532224     │
│ 10             │ 群馬県                 │ 大正 │ 9          │ 1920       │    │ 1052610      │ 514106     │ 538504     │
│ 11             │ 埼玉県                 │ 大正 │ 9          │ 1920       │    │ 1319533      │ 641161     │ 678372     │
│ 12             │ 千葉県                 │ 大正 │ 9          │ 1920       │    │ 1336155      │ 656968     │ 679187     │
│ 13             │ 東京都                 │ 大正 │ 9          │ 1920       │    │ 3699428      │ 1952989    │ 1746439    │
│ 14             │ 神奈川県               │ 大正 │ 9          │ 1920       │    │ 1323390      │ 689751     │ 633639     │
│ 15             │ 新潟県                 │ 大正 │ 9          │ 1920       │    │ 1776474      │ 871532     │ 904942     │
│ 16             │ 富山県                 │ 大正 │ 9          │ 1920       │    │ 724276       │ 354775     │ 369501     │
│ 17             │ 石川県                 │ 大正 │ 9          │ 1920       │    │ 747360       │ 364375     │ 382985     │
│ 18             │ 福井県                 │ 大正 │ 9          │ 1920       │    │ 599155       │ 293181     │ 305974     │
│ 19             │ 山梨県                 │ 大正 │ 9          │ 1920       │    │ 583453       │ 290817     │ 292636     │
│ 20             │ 長野県                 │ 大正 │ 9          │ 1920       │    │ 1562722      │ 758639     │ 804083     │
│ 21             │ 岐阜県                 │ 大正 │ 9          │ 1920       │    │ 1070407      │ 536334     │ 534073     │
│ 22             │ 静岡県                 │ 大正 │ 9          │ 1920       │    │ 1550387      │ 774169     │ 776218     │
│ 23             │ 愛知県                 │ 大正 │ 9          │ 1920       │    │ 2089762      │ 1033860    │ 1055902    │
│ 24             │ 三重県                 │ 大正 │ 9          │ 1920       │    │ 1069270      │ 525957     │ 543313     │
│ 25             │ 滋賀県                 │ 大正 │ 9          │ 1920       │    │ 651050       │ 313737     │ 337313     │
│ 26             │ 京都府                 │ 大正 │ 9          │ 1920       │    │ 1287147      │ 650780     │ 636367     │
│ 27             │ 大阪府                 │ 大正 │ 9          │ 1920       │    │ 2587847      │ 1344846    │ 1243001    │
│ 28             │ 兵庫県                 │ 大正 │ 9          │ 1920       │    │ 2301799      │ 1175426    │ 1126373    │
│ 29             │ 奈良県                 │ 大正 │ 9          │ 1920       │    │ 564607       │ 280383     │ 284224     │
│ 30             │ 和歌山県               │ 大正 │ 9          │ 1920       │    │ 750411       │ 372058     │ 378353     │
│ 31             │ 鳥取県                 │ 大正 │ 9          │ 1920       │    │ 454675       │ 222802     │ 231873     │
│ 32             │ 島根県                 │ 大正 │ 9          │ 1920       │    │ 714712       │ 354959     │ 359753     │
│ 33             │ 岡山県                 │ 大正 │ 9          │ 1920       │    │ 1217698      │ 605316     │ 612382     │
│ 34             │ 広島県                 │ 大正 │ 9          │ 1920       │    │ 1541905      │ 775080     │ 766825     │
│ 35             │ 山口県                 │ 大正 │ 9          │ 1920       │    │ 1041013      │ 521041     │ 519972     │
│ 36             │ 徳島県                 │ 大正 │ 9          │ 1920       │    │ 670212       │ 331768     │ 338444     │
│ 37             │ 香川県                 │ 大正 │ 9          │ 1920       │    │ 677852       │ 336195     │ 341657     │
│ 38             │ 愛媛県                 │ 大正 │ 9          │ 1920       │    │ 1046720      │ 515389     │ 531331     │
│ 39             │ 高知県                 │ 大正 │ 9          │ 1920       │    │ 670895       │ 332087     │ 338808     │
│ 40             │ 福岡県                 │ 大正 │ 9          │ 1920       │    │ 2188249      │ 1116818    │ 1071431    │
│ 41             │ 佐賀県                 │ 大正 │ 9          │ 1920       │    │ 673895       │ 329962     │ 343933     │
│ 42             │ 長崎県                 │ 大正 │ 9          │ 1920       │    │ 1136182      │ 584234     │ 551948     │

おお、できてる!
こんな感じでテーブルが表示されるようだ。

おわりに

コマンドでcsvを閲覧できるのは便利かも。
小さいファイルの確認であればこれを見て確認するのが良さそうだな〜。