情景:
有一個包含多個系的學生詳細資料的csv檔案,需要將檔案的內容插入到一個資料表中,保證每一個系產生一個單獨的排名列表。
學習資訊表 studentdata.csv 檔案的資料如下:
1,Navin M,98,CS2,Kavya N,70,CS3,Nawaz O,80,CS4,Hari S,80,EC5,Alex M,50,EC6,Neenu J,70,EC7,Bob A,30,EC8,Anu M,90,AE9,Sruthi,89,AE10,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 tablesUSER="root"PASS="123456"SOCKET="/data/mysqldata/3306/mysql.sock"mysql -u $USER -p$PASS -S $SOCKET <<EOF 2> /dev/nullcreate database students;EOF[ $? -eq 0 ] && echo Created DB || echo DB already existmysql -u $USER -p$PASS -S $SOCKET students <<EOF 2> /dev/nullcreate table students(id int,name varchar(100),mark int,dept varchar(4));EOF[ $? -eq 0 ] && echo Created table students || echo Table students already existmysql -u $USER -p$PASS -S $SOCKET students <<EOFdelete from students;EOF
指令碼二、將資料插入資料表的指令碼如下:
#!/bin/bash#filename : write_to_db.sh#use : read data from csv files and insert into mysql dbUSER="root"PASS="123456"SOCKET="/data/mysqldata/3306/mysql.sock"if [ $# -ne 1 ];then echo $0 DATAFILE echo exit 2fidata=$1while 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 <<EOFinsert into students values($query);EOFdone< $dataecho Wrote data into DB
指令碼三、查詢資料庫的指令碼如下:
#!/bin/bash#filename: read_db.sh#use : read data from mysql dbUSER="root"PASS="123456"SOCKET="/data/mysqldata/3306/mysql.sock"depts=`mysql -u $USER -p$PASS -S $SOCKET students <<EOF | tail -n +2select distinct dept from students;EOF`for d in $depts;doecho Department : $dresult="`mysql -u $USER -p$PASS -S $SOCKET students <<EOFSET @i:=0;select @i:=@i+1 as rank,name,mark from students where dept="$d" order by mark desc;EOF`"echo "$result"echo done
指令碼按照順序執行結果如下:
# chmod +x create_db.sh write_to_db.sh read_db.sh# ./create_db.sh DB already existTable students already exist# # ./write_to_db.sh studentdata.csv Wrote data into DB# # ./read_db.sh Department : CSrank name mark1 Navin M 982 Nawaz O 803 Kavya N 70Department : ECrank name mark1 Hari S 802 Neenu J 703 Alex M 504 Bob A 30Department : AErank name mark1 Anu M 902 Sruthi 893 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 namesYou can turn off this feature to get a quicker startup with -ADatabase changedmysql> 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>