RegEx从MySQL转储中删除行

时间:2016-10-28 23:08:17

标签: regex bash sed

CREATE TABLE `transaction` (
  `id` mediumint(9) NOT NULL AUTO_INCREMENT,
  `description` text NOT NULL,
  PRIMARY KEY (`id`),
  FULLTEXT KEY `description` (`description`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

如何从上面的MySQL转储中删除FULLTEXT行,并在行之前删除逗号,所以它看起来像这样:

CREATE TABLE `transaction` (
  `id` mediumint(9) NOT NULL AUTO_INCREMENT,
  `description` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

使用sed删除应该很容易,但我不确定如何在上面的行中删除此逗号,以便成功导入转储:

sed -i '/FULLTEXT KEY.*/d' dump.sql

有时候还有更多列有FULLTEXT索引:

CREATE TABLE `entity` (
  `id` mediumint(9) NOT NULL AUTO_INCREMENT,
  `company_name` varchar(255) NOT NULL DEFAULT '',
  `description` text NOT NULL,
  PRIMARY KEY (`id`),
  FULLTEXT KEY `company_name` (`company_name`),
  FULLTEXT KEY `description` (`description`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

3 个答案:

答案 0 :(得分:3)

Sed脚本:

#!/bin/bash    
sed -e '/FULLTEXT/d' |    
sed -ne '
/ENGINE=InnoDB/!{H}
/ENGINE=InnoDB/{x; s/,[ \t]*$//; p; }
${g;p;}
'

输入:

CREATE TABLE `transaction` (
  `id` mediumint(9) NOT NULL AUTO_INCREMENT,
  `description` text NOT NULL,
  PRIMARY KEY (`id`),
  FULLTEXT KEY `description` (`description`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


CREATE TABLE `entity` (
  `id` mediumint(9) NOT NULL AUTO_INCREMENT,
  `company_name` varchar(255) NOT NULL DEFAULT '',
  `description` text NOT NULL,
  PRIMARY KEY (`id`),
  FULLTEXT KEY `company_name` (`company_name`),
  FULLTEXT KEY `description` (`description`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

示例运行:

/home/user> ./1.sed < input

CREATE TABLE `transaction` (
  `id` mediumint(9) NOT NULL AUTO_INCREMENT,
  `description` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `entity` (
  `id` mediumint(9) NOT NULL AUTO_INCREMENT,
  `company_name` varchar(255) NOT NULL DEFAULT '',
  `description` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

答案 1 :(得分:2)

sed用于单个行上的简单替换,即全部。对于其他任何事情,你应该使用awk的清晰度,简洁性,可移植性,效率,稳健性和大多数其他理想的软件质量。除了s,g和p(以及-n)之外的所有sed构造都在20世纪70年代中期被淘汰,当时awk被发明并且仅存在于心理锻炼中。

鉴于此输入:

$ cat file
CREATE TABLE `transaction` (
  `id` mediumint(9) NOT NULL AUTO_INCREMENT,
  `description` text NOT NULL,
  PRIMARY KEY (`id`),
  FULLTEXT KEY `description` (`description`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `entity` (
  `id` mediumint(9) NOT NULL AUTO_INCREMENT,
  `company_name` varchar(255) NOT NULL DEFAULT '',
  `description` text NOT NULL,
  PRIMARY KEY (`id`),
  FULLTEXT KEY `company_name` (`company_name`),
  FULLTEXT KEY `description` (`description`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

使用GNU awk进行多字符RS:

$ awk -v RS=',\\s*FULLTEXT[^\n]*)' -v ORS= '1' file
CREATE TABLE `transaction` (
  `id` mediumint(9) NOT NULL AUTO_INCREMENT,
  `description` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `entity` (
  `id` mediumint(9) NOT NULL AUTO_INCREMENT,
  `company_name` varchar(255) NOT NULL DEFAULT '',
  `description` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

由于您正在使用GNU sed(对于-i),我假设您使用GNU awk没有问题,如果您需要就地编辑,请在开始时添加-i inplace

答案 2 :(得分:1)

awk -vRS=";"  'NF{gsub(/),\n +FULLTEXT.*)/,")\n)",$0);$0=$0";"}1' file