使用 AWK 查找并替换
Find with AWK and Replace
我有一个名为 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,
之间介绍一个space
所以我尝试了下面的命令
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');
awk解决方案:
awk '{ gsub(/, */, ", ", [=10=]) }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(/, */, ", ", [=12=])
- 将每个分隔符(即 ,
)替换为/重新排列 =29=]
只给后面的参考:
awk '{gsub(/,[0-9a-zA-Z]/, " &"); print}' sso_med.txt
我有一个名为 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');
awk解决方案:
awk '{ gsub(/, */, ", ", [=10=]) }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(/, */, ", ", [=12=])
- 将每个分隔符(即,
)替换为/重新排列 =29=]
只给后面的参考:
awk '{gsub(/,[0-9a-zA-Z]/, " &"); print}' sso_med.txt