[英]Bash - Find and replace regex with another string
I have the following string libVersion = '1.23.45.6'
and I need to replace 1.23.45.6
with 1.23.45.7
. 我有以下字符串
libVersion = '1.23.45.6'
,我需要将1.23.45.6
替换为1.23.45.7
。
Obviously the version could be any number with similar format (it does not have to be 4 numbers). 显然,该版本可以是格式相似的任何数字(不一定是4个数字)。
I tried to use the following but doesn't work 我尝试使用以下内容,但不起作用
echo "libVersion = '1.23.45.6'" |sed "s/([0-9\\.]+)/1.23.45.7/g"
Basic sed, ie sed without any arguments uses BRE
(Basic Regular Expression). 基本sed,即不带任何参数的sed使用
BRE
(基本正则表达式)。 In BRE
, you have to escape +
, to bring the power of regex +
which repeats the previous token one or more times, likewise for the capturing groups \\(regex\\)
在
BRE
,您必须转义+
,以使regex +
重复一次或多次重复先前的标记,对于捕获组\\(regex\\)
echo "libVersion = '1.23.45.6'" | sed "s/[0-9.]\+/1.23.45.7/"
You may also use a negated char class to replace all the chars exists within single quotes. 您也可以使用否定的char类来替换单引号中存在的所有char。
echo "libVersion = '1.23.45.6'" | sed "s/'[^']*'/'1.23.45.7'/"
Since the replacement should occur only one time, you don't need a g
global modifier. 由于替换只发生一次,因此不需要
g
全局修饰符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.