1

I am trying to match the below pattern which has spaces in between.

         -DsomeArg=some value 

and to replace it with below pattern using sed command.

         -DsomeArg="some value"

There can be any number of spaces in between. I tried below command but it's not working.

          sed 's/^\(-D.*\)=(.\+\s\+.\+)/\1="\2"/' test.dat

where as .* works instead of .+ , but I want to match one or more pattern. I am not able to find what I am doing wrong.

2
  • What is the end of the value? End of line? Commented Jan 15, 2013 at 19:09
  • 1
    What's wrong with sed 's/\(-D.*=\)\(.*\)/\1"\2"/'? Commented Jan 15, 2013 at 19:09

2 Answers 2

1
cat /tmp/test | sed -r 's/^(-D.*?)=(.+\s+.+)/\1="\2"/'
-DsomeArg="some value"

Hope that will help :)

Sign up to request clarification or add additional context in comments.

Comments

0

This does what you asked for:

sed 's/=\(.*\)/="\1"/'

or a little shorter:

sed 's/=/="/;s/$/"/'

But I have a feeling there's more to the story than you're saying. Like, are you expecting some text to follow "some value" that you want to leave outside of the quotation marks? Does something precede -DsomeArg that you don't want to match?

With regular expressions, context is everything.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.