sed 在匹配之后和之前替换字符串

sed 在匹配之后和之前替换字符串

我有一个包含一些行的文件(基本上是 MySQL 查询)。

例如:

GRANT ALL PRIVILEGES ON *.* TO 'user1'@'%' IDENTIFIED BY PASSWORD '*A576A2D299CFAF43CE15363F20BDD260FC1E9F77' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON *.* TO 'user2'@'%' IDENTIFIED BY PASSWORD '*CE15363F20BA576F43DD260FC1E9F77A2D299CFA';

IDENTIFIED我想删除从和开始的所有字符在第二个单引号之前

预期输出:

GRANT ALL PRIVILEGES ON *.* TO 'user1'@'%' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON *.* TO 'user2'@'%';

有 AWK 或 SED 命令吗?

答案1

您要求的内容(删除从 IDENTIFIED 开始和第二个单引号之前的所有字符):

$ sed "s/IDENTIFIED[^']*'[^']*//" file
GRANT ALL PRIVILEGES ON *.* TO 'user1'@'%' ' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON *.* TO 'user2'@'%' ';

实际上会产生您预期的输出:

$ sed "s/ *IDENTIFIED.*'//" file
GRANT ALL PRIVILEGES ON *.* TO 'user1'@'%' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON *.* TO 'user2'@'%';

相关内容