kuroの覚え書き

96の個人的覚え書き

TSV(CSV)から特定列のみ抜き出したファイルを書き出す

$ cut -f 1,2,3,5,6,7,8,9,10,11,27,28,29,30,31,32,33,34,35,316,327,329 input.txt > output.txt

これだけでOK。400カラム、4000行くらいのtsvから26列抜き出すのも一瞬。
csvからやる場合は

-d ","

をオプションに加える。

tsvをcsvに書き換えるには

cat sample.tsv | tr "\t" "," > sample.csv

とtrコマンドを使うのが良いらしい。

さらに1列目と2列目をつなげるなら

$ cut -f 1 input.txt > output1.txt
$ cut -f 2 input.txt > output2.txt
$ paste -d : output1.txt output2.txt > output3.txt

こうすると1:50000のように:を挟んでつなげてくれる。

2列目と3列目をつなげる。
1列目+つなげた2,3列目+4列目以降をもう一度くっつける。
CSVで書き出す。
なら

$ cut -f 1 input.txt > output1.txt
$ cut -f 2 input.txt > output2.txt
$ cut -f 3 input.txt > output3.txt
$ cut -f 4,5,6,....  input.txt > output4.txt
$ paste -d : output2.txt output3.txt > output5.txt
$ paste  output1.txt output5.txt output4.txt > output6.txt
$ cat  output6.txt | tr "\t" "," > output.csv

こんな感じでやればちょいちょいっと出力できちゃうかな。

#!/bin/sh
for file in "$@"
do
cut -f 1 $file > output1.txt
cut -f 2 $file > output2.txt
cut -f 3 $file > output3.txt
cut -f 327 $file > output4.txt
cut -f 329 $file > output5.txt
cut -f 5,6,7,8,9,10,11,27,28,29,30,31,32,33,34,35,316,331 $file > output6.txt
paste -d : output2.txt output3.txt > output7.txt
paste  output1.txt output4.txt output7.txt output6.txt output5.txt > output.tsv
rm output*.txt

こんな感じで自動化

先頭行はSQLiteにインポートするとき要らないので

$ sed -e '1d' output.tsv > output1.tsv 

としてやれば、もうすぐにインポートまでできるね。