shell編程系列24--shell操做數據庫實戰之利用shell腳本將文本數據導入到mysql中 利用shell腳本將文本數據導入到mysql中 需求1:處理文本中的數據,將文本中的數據插入到mysql中 1010 jerry 1991-12-13 male 1011 mike 1991-12-13 female 1012 tracy 1991-12-13 male 1013 kobe 1991-12-13 male 1014 allen 1991-12-13 female 1015 curry 1991-12-13 male 1016 tom 1991-12-13 female # 建立表結構和student同樣結構的student1表 MariaDB [school]> create table student1 like student; [root@localhost shell]# cat data.txt 1010 jerry 1991-12-13 male 1011 mike 1991-12-13 female 1012 tracy 1991-12-13 male 1013 kobe 1991-12-13 male 1014 allen 1991-12-13 female 1015 curry 1991-12-13 male 1016 tom 1991-12-13 female # 編寫導入數據腳本 [root@localhost shell]# cat import_mysql.sh #!/bin/bash # user="dbuser" password="123456" host="10.11.0.215" mysql_conn="mysql -h"$host" -u"$user" -p"$password"" cat data.txt | while read id name birth sex do $mysql_conn -e "INSERT INTO school.student1 values('$id','$name','$birth','$sex')" done [root@localhost shell]# [root@localhost shell]# sh import_mysql.sh [root@localhost shell]# mysql Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 53 Server version: 5.5.60-MariaDB MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> select * from school.student1; +------+--------+------------+--------+ | s_id | s_name | s_birth | s_sex | +------+--------+------------+--------+ | 1010 | jerry | 1991-12-13 | male | | 1011 | mike | 1991-12-13 | female | | 1012 | tracy | 1991-12-13 | male | | 1013 | kobe | 1991-12-13 | male | | 1014 | allen | 1991-12-13 | female | | 1015 | curry | 1991-12-13 | male | | 1016 | tom | 1991-12-13 | female | +------+--------+------------+--------+ # 導入數據能夠用load,有時候有一些特殊需求好比插入s_id大於1014的行,這個時候就須要使用 shell 語句進行過濾了 [root@localhost shell]# cat import_mysql.sh #!/bin/bash # user="dbuser" password="123456" host="10.11.0.215" mysql_conn="mysql -h"$host" -u"$user" -p"$password"" cat data.txt | while read id name birth sex do # 有插入條件 if [ $id -gt 1014 ];then $mysql_conn -e "INSERT INTO school.student1 values('$id','$name','$birth','$sex')" fi done [root@localhost shell]# sh import_mysql.sh [root@localhost shell]# sh operate_mysql.sh school "select * from student1" s_id s_name s_birth s_sex 1015 curry 1991-12-13 male 1016 tom 1991-12-13 female 需求2: 2021|hao|1989-12-21|male 2022|zhang|1989-12-21|male 2023|ouyang|1989-12-21|male 2024|li|1989-12-21|female [root@localhost shell]# cat import_mysql-2.sh #!/bin/bash # user="dbuser" password="123456" host="10.11.0.215" # IFS是系統自帶的變量,分隔符 input filre saperator IFS="|" cat data2.txt | while read id name birth sex do # 注意,當使用|相似這種特殊符號時,須要將mysql命令不寫成命令,不然會報錯 mysql -u"$user" -p"$password" -h"$host" -e "INSERT INTO school.student2 values('$id','$name','$birth','$sex')" done # # 使用冒號: 分隔也沒有問題 [root@localhost shell]# cat data3.txt 2025:hao:1989-12-21:male 2026:zhang:1989-12-21:male 2027:ouyang:1989-12-21:male 2028:li:1989-12-21:female [root@localhost shell]# sh operate_mysql.sh school "select * from student2" +------+--------+------------+--------+ | s_id | s_name | s_birth | s_sex | +------+--------+------------+--------+ | 2025 | hao | 1989-12-21 | male | | 2026 | zhang | 1989-12-21 | male | | 2027 | ouyang | 1989-12-21 | male | | 2028 | li | 1989-12-21 | female | +------+--------+------------+--------+ [root@localhost shell]# cat import_mysql-2.sh #!/bin/bash # user="dbuser" password="123456" host="10.11.0.215" #mysql_conn="mysql -h"$host" -u"$user" -p"$password"" # IFS是系統自帶的變量,分隔符 input filre saperator IFS=":" cat data3.txt | while read id name birth sex do mysql -u"$user" -p"$password" -h"$host" -e "INSERT INTO school.student2 values('$id','$name','$birth','$sex')" done