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

csvのカラムを簡単に並べ替える

Posted at

なぜやるのか

そこそこの行数のcsvを頻繁に扱う案件があり、読み込むデータの統合を簡単にやりたかった。
pandasの様なライブラリを使えば出来るが、より簡単にやりたかった。

どうやるのか

csvkitに含まれるcsvcutを使う。
インスコ方法などはググってください。

$ zsh --version
zsh 5.9 (x86_64-apple-darwin21.3.0)

読み込むファイル(sample.csv)

column_A,column_B,column_C,column_E,column_F,column_D
0,1,2,4,5,3
$ csvcut -c column_A,column_B,column_C,column_D,column_E,column_F sample.csv 
column_A,column_B,column_C,column_D,column_E,column_F
0,1,2,3,4,5

結果を別ファイルとかにしたいときはリダイレクト使う

$ csvcut -c column_A,column_B,column_C,column_D,column_E,column_F sample.csv > result.csv

標準出力とかコマンドの結果を入力にしたいときはパイプで

$  cat sample.csv |csvcut -c column_A,column_B,column_C,column_D,column_E,column_F
column_A,column_B,column_C,column_D,column_E,column_F
0,1,2,3,4,5
0
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
0
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?