LoginSignup
0
0

More than 3 years have passed since last update.

csvファイルの列を連結して1列にする

Last updated at Posted at 2020-09-05

↓こんなcsvがあった時、

a,1,あ
b,2,い
c,3,う

↓こんな感じで列単位で垂直に連結したい時があった。

a
b
c
1
2
3
あ
い
う

その方法を調べても出てこなかったので、下記のように実現した。

# 使い方は $ concat_csv_columns_vertically /path/to/your/csvfile

concat_csv_columns_vertically () {
    for i in `seq 1 $(awk -F, '{ print NF; exit }' $1)`; 
        do cut -d, -f$i $1; 
    done
}

処理の内容としては、csvの列数を取得した上でcutコマンドで列を左から順に抽出しているだけ。

これの1行版は↓

$ csvfile=/path/to/your/csvfile &&  for i in `seq 1 $(awk -F, '{ print NF; exit }' ${csvfile})`; do cut -d, -f$i $csvfile; done
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