我有一个名为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 (" >>> ) <<<
答案 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');