Sed替换所有字符直到第一个空格

时间:2022-11-16 16:52:53

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

我正在尝试替换配置文件中的一些路径。为此,我正在尝试使用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

我想替换/tmp/tmp/tmp/config并保留bla。bla不变。根据http://regexpal.com/和http://regexr.com?362 qc我应该使用

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

sed - e ' s / logging.config.file = ^ \[s]+ /日志\ config \。文件\ = \ /新/ g的文件

But it doesnt work.

但它不工作。

2 个解决方案

#1


2  

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

这将取代/ tmp / tmp /…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.

它在\1中“保存”到=的任何东西。然后把所有的东西都带到一个空间。最后“保存”\2中的其余字符串。

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

替换是通过响应\1 +“新字符串”+ \2完成的。

#2


1  

\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:

\s没有sed支持。此外,+必须被反切以获得它的特殊含义。我也会反斜杠点,以防止它们匹配任何东西:

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

or, shorter

或者,短

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

#1


2  

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

这将取代/ tmp / tmp /…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.

它在\1中“保存”到=的任何东西。然后把所有的东西都带到一个空间。最后“保存”\2中的其余字符串。

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

替换是通过响应\1 +“新字符串”+ \2完成的。

#2


1  

\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:

\s没有sed支持。此外,+必须被反切以获得它的特殊含义。我也会反斜杠点,以防止它们匹配任何东西:

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

or, shorter

或者,短

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