使用AWK和替换查找

时间:2017-05-25 20:43:57

标签: unix awk gsub gawk

我有一个名为sso_med.txt的文件

        insert into brs.user_components (user_name, component_id, created_date, created_by, last_updated_date, last_updated_by) values ('502683504',16,current_date, '502264160', current_date, '502264160');
        insert into brs.user_components (user_name, component_id, created_date, created_by, last_updated_date, last_updated_by) values ('502689031',15,current_date, '502264160', current_date, '502264160');
        insert into brs.user_components (user_name, component_id, created_date, created_by, last_updated_date, last_updated_by) values ('502689031',16,current_date, '502264160', current_date, '502264160');
        insert into brs.user_components (user_name, component_id, created_date, created_by, last_updated_date, last_updated_by) values ('212462117', 15, current_date, '502264160', current_date, '502264160');
        insert into brs.user_components (user_name, component_id, created_date, created_by, last_updated_date, last_updated_by) values ('212462117', 16, current_date, '502264160', current_date, '502264160');

我想在'502689031',16,current_date,

之间引入一个空格

所以我尝试了下面的命令

awk '{gsub(/,[0-9a-zA-Z]/, ", "); print}' sso_med.txt

但我没有得到我期待的东西

insert into brs.user_components (user_name, component_id, created_date, created_by, last_updated_date, last_updated_by) values ('502683504', 6, urrent_date, '502264160', current_date, '502264160'); 
insert into brs.user_components (user_name, component_id, created_date, created_by, last_updated_date, last_updated_by) values ('502689031', 5, urrent_date, '502264160', current_date, '502264160'); 
insert into brs.user_components (user_name, component_id, created_date, created_by, last_updated_date, last_updated_by) values ('502689031', 6, urrent_date, '502264160', current_date, '502264160');

2 个答案:

答案 0 :(得分:2)

awk 解决方案:

awk '{ gsub(/, */, ", ", $0) }1' sso_med.txt

输出:

insert into brs.user_components (user_name, component_id, created_date, created_by, last_updated_date, last_updated_by) values ('502683504', 16, current_date, '502264160', current_date, '502264160');
insert into brs.user_components (user_name, component_id, created_date, created_by, last_updated_date, last_updated_by) values ('502689031', 15, current_date, '502264160', current_date, '502264160');
insert into brs.user_components (user_name, component_id, created_date, created_by, last_updated_date, last_updated_by) values ('502689031', 16, current_date, '502264160', current_date, '502264160');
  • gsub(/, */, ", ", $0) - 使用单个尾随空格替换/ 重新排列 每个分隔符(即,

答案 1 :(得分:0)

请给出后面的参考:

awk '{gsub(/,[0-9a-zA-Z]/, " &"); print}' sso_med.txt