LoginSignup
2
2

More than 5 years have passed since last update.

めんどうなSQLをシェルスクリプトで自動化し、csvで書き出す

Posted at

はじめに

  • 繰り返しSQL打つのは手間
  • しかも、csvに書き出してゴニョゴニョするかもなー... って人のために

やってること

  1. SQLを叩き
  2. CSVで確認するために、カンマ区切りにし
  3. Fileに書き出す!
create_csv.sh

echo "start sql!"
output_path="/home/banaoh/Development/exp/" #任意のPath

for motion_id in 0 1 2 3 4 5 6 7
do
  for side_id in 0 1 2
  do
    mysql -u <user_name> <使用するデータベース名> -e
    # 1. sqlの発行
    "select *
     from <テーブル名> as t
     where motion_id = ${motion_id} and side_id = ${side_id}"
    # 2. カンマ区切りにする
     | sed -e "s/\t/,/g"
    # 3. csv fileに書き出し
     > "${output_path}${motion_id}_${side_id}.csv" 

    echo "Current output file is ${output_path}${motion_id}_${side_id}.csv"
  done
done

echo "end sql!"

使ってみる


banaoh@ubuntu:~$ sh create_csv.sh
start sql!
Current output file is/home/banaoh/Development/exp/0_0.csv
Current output file is/home/banaoh/Development/exp/0_1.csv
Current output file is/home/banaoh/Development/exp/0_2.csv
Current output file is/home/banaoh/Development/exp/1_0.csv
Current output file is/home/banaoh/Development/exp/1_1.csv
Current output file is/home/banaoh/Development/exp/1_2.csv
Current output file is/home/banaoh/Development/exp/2_0.csv
Current output file is/home/banaoh/Development/exp/2_1.csv
Current output file is/home/banaoh/Development/exp/2_2.csv
Current output file is/home/banaoh/Development/exp/3_0.csv
Current output file is/home/banaoh/Development/exp/3_1.csv
Current output file is/home/banaoh/Development/exp/3_2.csv
Current output file is/home/banaoh/Development/exp/4_0.csv
Current output file is/home/banaoh/Development/exp/4_1.csv
Current output file is/home/banaoh/Development/exp/4_2.csv
Current output file is/home/banaoh/Development/exp/5_0.csv
Current output file is/home/banaoh/Development/exp/5_1.csv
Current output file is/home/banaoh/Development/exp/5_2.csv
Current output file is/home/banaoh/Development/exp/6_0.csv
Current output file is/home/banaoh/Development/exp/6_1.csv
Current output file is/home/banaoh/Development/exp/6_2.csv
Current output file is/home/banaoh/Development/exp/7_0.csv
Current output file is/home/banaoh/Development/exp/7_1.csv
Current output file is/home/banaoh/Development/exp/7_2.csv
end sql!
banaoh@ubuntu:~$
2
2
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
2
2