shell编程系列24--shell操作数据库实战之利用shell脚本将文本数据导入到mysql中

利用shell脚本将文本数据导入到mysql中

    需求1:处理文本中的数据,将文本中的数据插入到mysql中
jerry -- male
mike -- female
tracy -- male
kobe -- male
allen -- female
curry -- male
tom -- female # 创建表结构和student一样结构的student1表
MariaDB [school]> create table student1 like student; [root@localhost shell]# cat data.txt
jerry -- male
mike -- female
tracy -- male
kobe -- male
allen -- female
curry -- male
tom -- female
# 编写导入数据脚本
[root@localhost shell]# cat import_mysql.sh
#!/bin/bash
# user="dbuser"
password=""
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
Server version: 5.5.-MariaDB MariaDB Server Copyright (c) , , 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 |
+------+--------+------------+--------+
| | jerry | -- | male |
| | mike | -- | female |
| | tracy | -- | male |
| | kobe | -- | male |
| | allen | -- | female |
| | curry | -- | male |
| | tom | -- | female |
+------+--------+------------+--------+ # 导入数据可以用load,有时候有一些特殊需求比如插入s_id大于1014的行,这个时候就需要使用 shell 语句进行过滤了
[root@localhost shell]# cat import_mysql.sh
#!/bin/bash
# user="dbuser"
password=""
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 ];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
curry -- male
tom -- female 需求2:
|hao|--|male
|zhang|--|male
|ouyang|--|male
|li|--|female [root@localhost shell]# cat import_mysql-.sh
#!/bin/bash
# user="dbuser"
password=""
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
:hao:--:male
:zhang:--:male
:ouyang:--:male
:li:--:female [root@localhost shell]# sh operate_mysql.sh school "select * from student2"
+------+--------+------------+--------+
| s_id | s_name | s_birth | s_sex |
+------+--------+------------+--------+
| | hao | -- | male |
| | zhang | -- | male |
| | ouyang | -- | male |
| | li | -- | female |
+------+--------+------------+--------+
[root@localhost shell]# cat import_mysql-.sh
#!/bin/bash
# user="dbuser"
password=""
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

shell编程系列24--shell操作数据库实战之利用shell脚本将文本数据导入到mysql中的更多相关文章

  1. shell编程系列13--文本处理三剑客之sed利用sed追加文件内容

    shell编程系列13--文本处理三剑客之sed利用sed追加文件内容 追加用法总结: .a 在匹配行后面追加 .i 在匹配行前面追加 .r 将文件内容追加到匹配行后面 .w 将匹配行写入指定文件 追 ...

  2. shell编程系列12--文本处理三剑客之sed利用sed修改文件内容

    shell编程系列12--文本处理三剑客之sed利用sed修改文件内容 修改命令对照表 编辑命令 1s/old/new/ 替换第1行内容old为new ,10s/old/new/ 替换第1行到10行的 ...

  3. shell编程系列11--文本处理三剑客之sed利用sed删除文本中的内容

    shell编程系列11--文本处理三剑客之sed利用sed删除文本中的内容 删除命令对照表 命令 含义 1d 删除第一行内容 ,10d 删除1行到10行的内容 ,+5d 删除10行到16行的内容 /p ...

  4. shell编程系列10--文本处理三剑客之sed利用sed查询特定内容

    shell编程系列10--文本处理三剑客之sed利用sed查询特定内容 利用sed查找文件内容: pattern种类: .8p .,10p .,+5p ./regexp/p .,/regexp/p . ...

  5. Shell 脚本操作数据库实战

    安装mariadb 数据库  (默认没有密码,直接mysql即可进入数据库管理控制台) yum install mariadb mariadb-server mariadb-libs -y syste ...

  6. shell编程系列25--shell操作数据库实战之备份MySQL数据,并通过FTP将其传输到远端主机

    shell编程系列25--shell操作数据库实战之备份MySQL数据,并通过FTP将其传输到远端主机 备份mysql中的库或者表 mysqldump 常用参数详解: -u 用户名 -p 密码 -h ...

  7. shell编程系列23--shell操作数据库实战之mysql命令参数详解

    shell编程系列23--shell操作数据库实战之mysql命令参数详解 mysql命令参数详解 -u 用户名 -p 用户密码 -h 服务器ip地址 -D 连接的数据库 -N 不输出列信息 -B 使 ...

  8. shell编程系列22--shell操作数据库实战之shell脚本与MySQL数据库交互(增删改查)

    shell编程系列22--shell操作数据库实战之shell脚本与MySQL数据库交互(增删改查) Shell脚本与MySQL数据库交互(增删改查) # 环境准备:安装mariadb 数据库 [ro ...

  9. shell编程系列26--大型脚本工具开发实战

    shell编程系列26--大型脚本工具开发实战 大型脚本工具开发实战 拆分脚本功能,抽象函数 .function get_all_group 返回进程组列表字符串 .function get_all_ ...

随机推荐

  1. 生成1~n的排列(模板),生成可重集的排列(对应紫书P184, P185)

    生成1~n的排列: #include<iostream> using namespace std; void print_permutation(int n, int *A, int cu ...

  2. Vuex状态管理总结

    一.什么是 Vuex 1.Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式 2.Vuex 采用集中式存储和管理应用中所有组件的状态 3.Vuex 应用的核心是 store(仓库)-- 包 ...

  3. 微信小程序~基础组件

    (1)视图容器 名称 功能说明 movable-view 可移动的视图容器,在页面中可以拖拽滑动 cover-image 覆盖在原生组件之上的图片视图 cover-view 覆盖在原生组件之上的文本视 ...

  4. 30.第一个Linq 数据库查询

    使用Linq to Entity查询数据库 首先在项目中添加ADO.NET实体数据模型,如下 新建连接 勾选生成的表 点击完成即可看到两个建立的实体数据对象模型 模型生成好之后就可以直接使用了 Cus ...

  5. go实现文件的上传

    上传端 send.go package main import ( "fmt" "io" "net" "os") fun ...

  6. 指数基金介绍专栏(8):国企指数(H股指数)详细介绍,最新资料解析,看这一篇就够了

    作者:牛大 | 公众号:定投五分钟 大家好,我是牛大.每天五分钟,投资你自己:坚持基金定投,终会财富自由! 昨天牛大给大家介绍了恒生指数,没看的朋友可以去公众号看一下. 指数基金介绍专栏(7):恒生指 ...

  7. Kubernetes 学习18配置网络插件flannel

    一.概述 1.我们在学习docker时知道docker有四种常用的网络模型 a.bridge:桥接式网络 b.joined:联盟式网络,共享使用另外一个容器的网络名称空间 b.opened:容器直接共 ...

  8. 【转发】c#做端口转发程序支持正向连接和反向链接

    可以通过中转server来连接sql server,连接的时候用ip,port,不是冒号,是逗号 但试过local port 21想连接AS400的FTP却不成功...为咩涅... https://w ...

  9. javaweb上传文件夹

    我们平时经常做的是上传文件,上传文件夹与上传文件类似,但也有一些不同之处,这次做了上传文件夹就记录下以备后用. 首先我们需要了解的是上传文件三要素: 1.表单提交方式:post (get方式提交有大小 ...

  10. PHP CURL 错误码说明

    curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));//一般不加 <?php return [ '1'=> ...