在括号之间的单词周围插入空格

时间:2017-05-26 16:42:51

标签: bash unix awk gsub

我有一个名为sso-shop-med.txt的文件。

insert into brs.user_shops (user_name, shop_id, is_active, created_date, created_by, last_updated_date, last_updated_by)values('204026601',18, TRUE, current_date,'502675409',current_date,'502675409'); 
insert into brs.user_shops (user_name, shop_id, is_active, created_date, created_by, last_updated_date, last_updated_by)values('502689031',18, TRUE, current_date,'502675409',current_date,'502675409'); 
insert into brs.user_shops (user_name, shop_id, is_active, created_date, created_by, last_updated_date, last_updated_by)values('502683504',18, TRUE, current_date,'502675409',current_date,'502675409'); 
insert into brs.user_shops (user_name, shop_id, is_active, created_date, created_by, last_updated_date, last_updated_by) values ('204009239', 18, TRUE, current_date, '212585704', current_date, '212585704'); 
insert into brs.user_shops (user_name, shop_id, is_active, created_date, created_by, last_updated_date, last_updated_by) values ('204009292', 18, TRUE, current_date, '212585704', current_date, '212585704');

我想在)values(之间添加一个差距,如下所示:) values (

我编写了以下awk命令,但它无法正常工作

awk '{gsub(/")values("/, ") values (" ); print}' sso-shop-med.txt

结果错误:

awk: syntax error in regular expression ")values(" at values("
 source line number 1
 context is
    {gsub(/")values("/, ") values (" >>>  ) <<< 

5 个答案:

答案 0 :(得分:1)

在正则表达式中,你需要逃避parens并删除引号:

$ cat file
last_updated_by)values('204026601',
last_updated_by)values('502689031',
last_updated_by)values('502683504',
last_updated_by) values ('204009239',
last_updated_by) values ('204009292',

$ awk '{gsub(/\)values\(/, ") values (" ); print}' file
last_updated_by) values ('204026601',
last_updated_by) values ('502689031',
last_updated_by) values ('502683504',
last_updated_by) values ('204009239',
last_updated_by) values ('204009292',

答案 1 :(得分:0)

我不确定您的awk版本,但这似乎有效:

awk '{gsub("\\)values\\(", "\) values \(") ; print}'

insert into brs.user_shops (user_name, shop_id, is_active, created_date, created_by, last_updated_date, last_updated_by)values('204026601',18, TRUE, current_date,'502675409',current_date,'502675409');
insert into brs.user_shops (user_name, shop_id, is_active, created_date, created_by, last_updated_date, last_updated_by)values('502689031',18, TRUE, current_date,'502675409',current_date,'502675409');
insert into brs.user_shops (user_name, shop_id, is_active, created_date, created_by, last_updated_date, last_updated_by)values('502683504',18, TRUE, current_date,'502675409',current_date,'502675409');
insert into brs.user_shops (user_name, shop_id, is_active, created_date, created_by, last_updated_date, last_updated_by) values ('204009239', 18, TRUE, current_date, '212585704', current_date, '212585704');
insert into brs.user_shops (user_name, shop_id, is_active, created_date, created_by, last_updated_date, last_updated_by) values ('204009292', 18, TRUE, current_date, '212585704', current_date, '212585704');

成:

insert into brs.user_shops (user_name, shop_id, is_active, created_date, created_by, last_updated_date, last_updated_by) values ('204026601',18, TRUE, current_date,'502675409',current_date,'502675409');
insert into brs.user_shops (user_name, shop_id, is_active, created_date, created_by, last_updated_date, last_updated_by) values ('502689031',18, TRUE, current_date,'502675409',current_date,'502675409');
insert into brs.user_shops (user_name, shop_id, is_active, created_date, created_by, last_updated_date, last_updated_by) values ('502683504',18, TRUE, current_date,'502675409',current_date,'502675409');
insert into brs.user_shops (user_name, shop_id, is_active, created_date, created_by, last_updated_date, last_updated_by) values ('204009239', 18, TRUE, current_date, '212585704', current_date, '212585704');
insert into brs.user_shops (user_name, shop_id, is_active, created_date, created_by, last_updated_date, last_updated_by) values ('204009292', 18, TRUE, current_date, '212585704', current_date, '212585704');

看起来如何?

答案 2 :(得分:0)

gawk 方法:

awk '{ gsub(/ *values */," values ",$0) }1' sso-shop-med.txt

输出:

insert into brs.user_shops (user_name, shop_id, is_active, created_date, created_by, last_updated_date, last_updated_by) values ('204026601',18, TRUE, current_date,'502675409',current_date,'502675409'); 
insert into brs.user_shops (user_name, shop_id, is_active, created_date, created_by, last_updated_date, last_updated_by) values ('502689031',18, TRUE, current_date,'502675409',current_date,'502675409'); 
insert into brs.user_shops (user_name, shop_id, is_active, created_date, created_by, last_updated_date, last_updated_by) values ('502683504',18, TRUE, current_date,'502675409',current_date,'502675409'); 
insert into brs.user_shops (user_name, shop_id, is_active, created_date, created_by, last_updated_date, last_updated_by) values ('204009239', 18, TRUE, current_date, '212585704', current_date, '212585704'); 
insert into brs.user_shops (user_name, shop_id, is_active, created_date, created_by, last_updated_date, last_updated_by) values ('204009292', 18, TRUE, current_date, '212585704', current_date, '212585704');
  • gsub(/ *values */," values ",$0) - 将 重新排列 values字替换为前导/尾随空格(如果values周围有冗余空格 - 如果在values附近的任何一侧没有空间,它们将被挤压 - 它们将被添加)

答案 3 :(得分:0)

您必须使用\转义括号,因为它们是正则表达式中的控制字符。

当我说通常像锤子一样使用awk时,我觉得sed在这里更合适:

sed s/\)values\(/\)\ values\ \(/g sso-shop-med.txt

答案 4 :(得分:0)

在UNIX中使用Perl,可以解决这个问题。

use strict;
use warnings;
while(<>){
print if(s/\((values)\)/\ $1 \/);
}

输出:

insert into brs.user_shops (user_name, shop_id, is_active, created_date, created_by, last_updated_date, last_updated_by) values ('204026601',18, TRUE, current_date,'502675409',current_date,'502675409'); 
insert into brs.user_shops (user_name, shop_id, is_active, created_date, created_by, last_updated_date, last_updated_by) values ('502689031',18, TRUE, current_date,'502675409',current_date,'502675409'); 
insert into brs.user_shops (user_name, shop_id, is_active, created_date, created_by, last_updated_date, last_updated_by) values ('502683504',18, TRUE, current_date,'502675409',current_date,'502675409'); 
insert into brs.user_shops (user_name, shop_id, is_active, created_date, created_by, last_updated_date, last_updated_by) values ('204009239', 18, TRUE, current_date, '212585704', current_date, '212585704'); 
insert into brs.user_shops (user_name, shop_id, is_active, created_date, created_by, last_updated_date, last_updated_by) values ('204009292', 18, TRUE, current_date, '212585704', current_date, '212585704');