sromit
sromit

Reputation: 1112

Find and replace with AWK command

I have a file called sso_med.txt like

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');

But I want to make it like

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');

I am trying the awk command

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

But its not working.Any idea what I am doing wrong in Regex

Upvotes: 0

Views: 2488

Answers (3)

isosceleswheel
isosceleswheel

Reputation: 1546

To avoid removing similar patterns elsewhere in the string, use sub, which just replaces the first instance of the pattern:

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

Example:

$ 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}'

Result:

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);

Upvotes: -1

RavinderSingh13
RavinderSingh13

Reputation: 133518

try also sed version:

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

substituting the starting digits from 0 to 9 and then : to space with NULL and print the rest.

Upvotes: 0

karakfa
karakfa

Reputation: 67497

try

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

Upvotes: 2

Related Questions