情景:
有一個包含多個系的學生詳細信息的csv文件,需要將文件的內容插入到一個數據表中,保證每一個系生成一個單獨的排名列表。
學習信息表 studentdata.csv 文件的數據如下:
1,Navin M,98,CS 2,Kavya N,70,CS 3,Nawaz O,80,CS 4,Hari S,80,EC 5,Alex M,50,EC 6,Neenu J,70,EC 7,Bob A,30,EC 8,Anu M,90,AE 9,Sruthi,89,AE 10,Andrew,89,AE
腳本思路
這個問題有兩種處理思路,從shell腳本的角度看,可以用sort,awk等bash工具解決,也可以用一個sql數據庫的數據表也可以解決。
下面需要編寫3個腳本,分別用於創建數據庫及數據表、想數據表中插入學生數據、從數據表中讀取並顯示處理過的數據。
注意:下面腳本中mysql數據連接的user,pass,socket等變量是我測試環境中的連接,在使用腳本時可以根據情況修改;
另外對於實際導入的csv格式和行數不同,可以對照進行腳本中的數據庫名、表名、列名進行修改,這樣這三個腳本就可以解決這一類問題了。
腳本一、創建數據庫及數據表的腳本如下:
#!/bin/bash #filename : create_db.sh #use : create mysql database and tables USER="root" PASS="123456" SOCKET="/data/mysqldata/3306/mysql.sock" mysql -u $USER -p$PASS -S $SOCKET </dev/null create database students; EOF [ $? -eq 0 ] && echo Created DB || echo DB already exist mysql -u $USER -p$PASS -S $SOCKET students < /dev/null create table students( id int, name varchar(100), mark int, dept varchar(4) ); EOF [ $? -eq 0 ] && echo Created table students || echo Table students already exist mysql -u $USER -p$PASS -S $SOCKET students <
腳本二、將數據插入數據表的腳本如下:
#!/bin/bash #filename : write_to_db.sh #use : read data from csv files and insert into mysql db USER="root" PASS="123456" SOCKET="/data/mysqldata/3306/mysql.sock" if [ $# -ne 1 ]; then echo $0 DATAFILE echo exit 2 fi data=$1 while read line; do oldIFS=$IFS IFS=, values=($line) values[1]="\"`echo ${values[1]} | tr ' ' '#' `\"" values[3]="\"`echo ${values[3]}`\"" query=`echo ${values[@]} | tr ' #' ', ' ` IFS=$oldIFS mysql -u $USER -p$PASS -S $SOCKET students <
腳本三、查詢數據庫的腳本如下:
#!/bin/bash #filename: read_db.sh #use : read data from mysql db USER="root" PASS="123456" SOCKET="/data/mysqldata/3306/mysql.sock" depts=`mysql -u $USER -p$PASS -S $SOCKET students <
腳本按照順序執行結果如下:
# chmod +x create_db.sh write_to_db.sh read_db.sh # ./create_db.sh DB already exist Table students already exist # # ./write_to_db.sh studentdata.csv Wrote data into DB # # ./read_db.sh Department : CS rank name mark 1 Navin M 98 2 Nawaz O 80 3 Kavya N 70 Department : EC rank name mark 1 Hari S 80 2 Neenu J 70 3 Alex M 50 4 Bob A 30 Department : AE rank name mark 1 Anu M 90 2 Sruthi 89 3 Andrew 89
在MySQL數據庫中確認插入後結果:
mysql> mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | students | | test | +--------------------+ 5 rows in set (0.00 sec) mysql> mysql> use students; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> mysql> show tables; +--------------------+ | Tables_in_students | +--------------------+ | students | +--------------------+ 1 row in set (0.00 sec) mysql> mysql> select * from students; +------+---------+------+------+ | id | name | mark | dept | +------+---------+------+------+ | 1 | Navin M | 98 | CS | | 2 | Kavya N | 70 | CS | | 3 | Nawaz O | 80 | CS | | 4 | Hari S | 80 | EC | | 5 | Alex M | 50 | EC | | 6 | Neenu J | 70 | EC | | 7 | Bob A | 30 | EC | | 8 | Anu M | 90 | AE | | 9 | Sruthi | 89 | AE | | 10 | Andrew | 89 | AE | +------+---------+------+------+ 10 rows in set (0.00 sec) mysql>