查找并替换为AWK命令

时间:2017-05-25 19:12:37

标签: macos unix awk gsub gawk

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

197: insert into brs.user_components (user_name, component_id, created_date, created_by, last_updated_date,last_updated_by) values ('502683504',15,current_date, '502264160', current_date, '502264160'); 
198: 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'); 
199: 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 ('502683504',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 ('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');

我正在尝试使用awk命令

awk '{gsub(/^\d+:\s/,""); print}' sso_med.txt

但它不起作用。任何想法我在Regex中做错了什么

3 个答案:

答案 0 :(得分:1)

awk '{gsub(/^[0-9]+: /,"")}1' file

答案 1 :(得分:0)

尝试sed版本:

sed 's/^[0-9]*: //'  Input_file

将起始数字从0到9替换,然后:用空格替换空格并打印其余部分。

答案 2 :(得分:-1)

为避免删除字符串中其他位置的类似模式,请使用sub,它只替换模式的第一个实例:

# explanation of regex: "^"=line starts with pattern, "[0-9]+"=multiple numbers, ": "=colon and single space
awk '{sub(/^[0-9]+: /, "", $0); print $0}'

示例:

$ echo '197: insert into brs.user_components (user_name, component_id, created_date, created_by, last_updated_date,last_updated_by) values ('502683504',15,current_date, '502264160', current_date, '502264160');' | awk '{sub(/^[0-9]+: /, "", $0); print $0}'

结果:

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