实现数据库批量更新与回滚

create database awktest;

use awktest

create table user(
    id int unsigned not null unique auto_increment primary key,
    name varchar(40) not null,
    value int unsigned not null
)ENGINE=InnoDB DEFAULT CHARSET=utf8 comment='user表';

insert into user(id, name, value) values  ('1','xiao1','18'),
('2','xiao2','19'),
('3','xiao3','20'),
('4','xiao4','21');

exit

mysql -uroot -p -e 'use awktest;select * from user;' > datadump.txt

nl datadump.txt | sed '1d' | awk -F ' ' '{printf ("update user set value = 0 where name = \047%s\047;\n", $3)}' > sql.txt

nl datadump.txt | sed '1d' | awk -F ' ' '{printf ("update user set value = %s where name = \047%s\047;\n",$4,$3)}' > rollback.txt

db -Dawktest < ~/sql.txt
db -Dawktest < ~/rollback.txt

其中sql.txt:

update user set value = 0 where name = 'xiao1';
update user set value = 0 where name = 'xiao2';
update user set value = 0 where name = 'xiao3';
update user set value = 0 where name = 'xiao4';

rollback.txt:

update user set value = 18 where name = 'xiao1';
update user set value = 19 where name = 'xiao2';
update user set value = 20 where name = 'xiao3';
update user set value = 21 where name = 'xiao4';

sed awk 小例的更多相关文章

  1. sed awk 样例

    sed [options] '[action]' filename options: -n:一般sed命令会把所有数据都输出到屏幕,如果加入此选项,则只会把经过sed命令处理的行输出到屏幕. -e:允 ...

  2. linux 命令小例

    xargs示例: ls |xargs -i mv {}  /opt find示例: find -mtime +n -name “*.avi” -type f -exec rm {} \; find - ...

  3. Sed&awk笔记之sed篇

    http://blog.csdn.net/a81895898/article/details/8482387 Sed是什么 <sed and awk>一书中(1.2 A Stream Ed ...

  4. Sed&awk笔记之sed篇(转)

    Sed是什么 <sed and awk>一书中(1.2 A Stream Editor)是这样解释的: Sed is a "non-interactive" strea ...

  5. Sed文本替换一例

    使用 Sed 完成文本替换操作任务是非常合适的. 现在, 假设我要将一个原有 Java 项目中的一些包及下面的类移到另一个项目中复用. Project javastudy: Packages: alg ...

  6. [svc]linux正则实战(grep/sed/awk)

    企业实战: 过滤ip 过滤出第二行的 192.168.2.11. eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 ine ...

  7. Linux三剑客grep/sed/awk

    grep/sed/awk被称为linux的“三剑客” grep更适合单纯的查找或匹配文本: sed更适合编辑匹配到的文本: awk更适合格式化文本,对文本进行较复杂各式处理: Grep --color ...

  8. Ubuntu13.04 Eclipse下编译安装Hadoop插件及使用小例

    Ubuntu13.04 Eclipse下编译安装Hadoop插件及使用小例 一.在Eclipse下编译安装Hadoop插件 Hadoop的Eclipse插件现在已经没有二进制版直接提供,只能自己编译. ...

  9. linux sed awk seq 正则使用 截取字符 之技巧

    [root@room9pc01 ~]# seq 5 1 2 3 4 5 [root@room9pc01 ~]# seq 2 5 2 3 4 5 seq 1 2 10 1 3 5 7 9 [root@d ...

随机推荐

  1. js 对象 视频 插入元素

    <!DOCTYPE html><html lang="en"><head>  <meta charset="UTF-8" ...

  2. 导入excel表格的数据--->到mysql中

    01下载excel类,将Classes文件夹放入ThinkPHP\Extend\Vendor\位置 下载地址 http://phpexcel.codeplex.com/releases/view/26 ...

  3. AR9531的mac地址

    root@OpenWrt:/# ifconfigbr-lan    Link encap:Ethernet  HWaddr 00:11:22:34:56:78            inet addr ...

  4. web页面开发相关基础

    CSS是一种用于web的标准布局语言,可以控制版面.颜色以及元素和图像的大小和位置.HTML文档应该利用外部样式表来定义文档中使用的样式.JavaScript也应该放在外部文档中,这个文档应该只包含J ...

  5. 关于c++的引用

    引用的本质 引用事实上就是两个变量指向同一个地址 int x; int &y = x; cout << &x << endl; cout << &a ...

  6. chapter 12_2 保存无环的table

    保存table有几种方法,选用哪种方法取决于对table的结构作出了哪些限制性的假设 第一个方法: function serialize(o) if type(o) == "number&q ...

  7. 第三次冲刺spring会议(第四次会议)

    [例会时间]2014/5/23 21:15 [例会地点]9#446 [例会形式]轮流发言 [例会主持]马翔 [例会记录]兰梦 小组成员:兰梦 ,马翔,李金吉,赵天,胡佳奇

  8. Openjudge-计算概论(A)-大象喝水

    描述 一只大象口渴了,要喝20升水才能解渴,但现在只有一个深h厘米,底面半径为r厘米的小圆桶(h和r都是整数).问大象至少要喝多少桶水才会解渴. 输入输入有一行:包行两个整数,以一个空格分开,分别表示 ...

  9. qsort函数辅助函数compare函数的编写

    qsort的第四个参数,辅助函数compare的关于不同排序对象的不同写法: 一.对int类型数组排序 ]; int compare(const void *a, const void *b) { r ...

  10. The Linux Storage Stack Diagram 内核 4.0 版的 I/O 栈