nervosol
nervosol

Reputation: 1291

Sed replace all characters until first space

I am trying to replace some paths in config file. To do that I am trying to use sed.

My file looks something like that:

-Djava.util.logging.config.file=/tmp/tmp/tmp/config bla.bla

I want to replace /tmp/tmp/tmp/config and keep bla.bla untouched. According to http://regexpal.com/ and http://regexr.com?362qc I should use

sed -e 's/logging.config.file=[^\s]+/logging\.config\.file\=\/new/g' file

But it doesnt work.

Upvotes: 2

Views: 10672

Answers (2)

choroba
choroba

Reputation: 242038

\s is not supported by sed. Also, the + must be backslashed to get its special meaning. I would also backslash the dots to prevent them from matching anything:

sed -e 's/logging\.config\.file=[^[:space:]]\+/logging\.config\.file\=\/new/g'

or, shorter

sed -e 's%\(logging\.config\.file=\)[^[:space:]]\+%\1/new%g'

Upvotes: 1

fedorqui
fedorqui

Reputation: 290135

This will replace /tmp/tmp/... with aaa:

$ sed 's/\(.*=\)[^ ]* \(.*\)/\1 aaa \2/g' <<< "-Djava.util.logging.config.file=/tmp/tmp/tmp/config bla.bla"
-Djava.util.logging.config.file= aaa bla.bla

It "saves" anything up to = in \1. Then fetches everything up to an space. Finally "saves" the rest of the string in \2.

The replacement is done by echoing \1 + "new string" + \2.

Upvotes: 2

Related Questions