Hai Le Hai Le - 11 months ago 50
Bash Question

Adding Quote around an existing string in multiple files

I want to add a double quotation around an existing string in a multiples files.

check_envurl!ma100mlpapl002!-p 8080!-u /apollo/info?section=ERROR!!!!!

I want to add a quote after the first "-u" but before the / and before !. So it would be

check_envurl!ma100mlpapl002!-p 8080!-u "/apollo/info?section=ERROR"!!!!!

I've tried with sed replacement but doesn't meet the criteria

define service {
host_name ma100mlpapl002
servicegroups Environment URL
service_description http://ma100mlpapl002:8080/apollo/info?section=ERROR
display_name http://ma100mlpapl002:8080/apollo/info?section=ERROR
check_command check_envurl!ma100mlpapl002!-p 8080!-u /apollo/info?section=ERROR!!!!!
max_check_attempts 3
check_interval 15
retry_interval 1
check_period 24x7
contact_groups siteops

sed '/pattern/a some text here' filename


Answer Source

Try this:

$ sed 's/-u \([^!]*\)!/-u "\1"!/' <<< 'check_envurl!ma100mlpapl002!-p 8080!-u /apollo/info?section=ERROR!!!!!'
check_envurl!ma100mlpapl002!-p 8080!-u "/apollo/info?section=ERROR"!!!!!