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?

More than 3 years have passed since last update.

1対nのcsvを1対1に変換するシェルスクリプト

Posted at
1,hoge,hoge,....
2,fuga,fuga,....
3,piyo,piyo,....

みたいなcsvを

1,hoge
1,hoge
.....
2,fuga
2,fuga
.....
3,piyo
3,piyo

みたいに置き換えます。

sample.sh
cat $1 | while read line
do
arr=( `echo ${line} | tr -s ',' ' '` )
for ((i=0; i < ${#arr[@]}-1; i++)) {
  echo "${arr[0]},${arr[$i+1]}"
}
done

↑のsample.shをどこかに保存して、実行してcsvファイルか何かに吐きだしてあげればOKです。
実行コマンドはこんな具合に。

実行
$ sh sample.sh hoge.csv >> fuga.csv
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?