使用 AWK 命令查找和替换
Find and replace with AWK command
我有一个名为 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
但是 working.Any 不知道我在正则表达式中做错了什么
尝试
awk '{gsub(/^[0-9]+: /,"")}1' file
也试试 sed 版本:
sed 's/^[0-9]*: //' Input_file
将起始数字从 0 替换为 9 然后 : 到 space 为 NULL 并打印其余部分。
为避免删除字符串中其他地方的相似模式,请使用 sub
,它仅替换模式的第一个实例:
# explanation of regex: "^"=line starts with pattern, "[0-9]+"=multiple numbers, ": "=colon and single space
awk '{sub(/^[0-9]+: /, "", [=10=]); print [=10=]}'
示例:
$ 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]+: /, "", [=11=]); print [=11=]}'
结果:
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);
我有一个名为 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
但是 working.Any 不知道我在正则表达式中做错了什么
尝试
awk '{gsub(/^[0-9]+: /,"")}1' file
也试试 sed 版本:
sed 's/^[0-9]*: //' Input_file
将起始数字从 0 替换为 9 然后 : 到 space 为 NULL 并打印其余部分。
为避免删除字符串中其他地方的相似模式,请使用 sub
,它仅替换模式的第一个实例:
# explanation of regex: "^"=line starts with pattern, "[0-9]+"=multiple numbers, ": "=colon and single space
awk '{sub(/^[0-9]+: /, "", [=10=]); print [=10=]}'
示例:
$ 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]+: /, "", [=11=]); print [=11=]}'
结果:
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);