[英]Replace value containing quotes with sed command and regex
I am trying to substitute a new version in my yaml file using sed, but I cannot figure out what I am missing from this statement.我正在尝试使用 sed 替换我的 yaml 文件中的新版本,但我无法弄清楚我从这个语句中遗漏了什么。
I want to convert我想转换
version: "x.x.x"
to到
version: "z.z.z"
Here is my current sed Statement:这是我目前的 sed 声明:
sed -i "" "s|version: "[[:digit:]].[[:digit:]].[[:digit:]]"\|version: "${VERSION}"|" file.yaml
You may use您可以使用
sed -i "" 's|version: "[[:digit:]]\.[[:digit:]]\.[[:digit:]]"|version: "'"${VERSION}"'"|' file.yaml
To match one or more digits you may use \\{1,\\}
range quantifier after eacg [[:digit:]]
:要匹配一位或多位数字,您可以在 eacg
[[:digit:]]
之后使用\\{1,\\}
范围量词:
sed -i "" 's|version: "[[:digit:]]\{1,\}\.[[:digit:]]\{1,\}\.[[:digit:]]\{1,\}"|version: "'"${VERSION}"'"|' file.yaml
Details细节
version: "[[:digit:]]\\.[[:digit:]]\\.[[:digit:]]"
- LHS, matches version:
, space, "
, a digit, .
, a digit, .
and a digit (digits if \\{1,\\}
is used) version: "[[:digit:]]\\.[[:digit:]]\\.[[:digit:]]"
- LHS, 匹配version:
, space, "
, a digit, .
, a digit, .
and一个数字(如果使用\\{1,\\}
则为数字)version: "'"${VERSION}"'"
- RHS, replacement, replaces with version:
, space, "
, VERSION
variable contents, and a "
. version: "'"${VERSION}"'"
- RHS,替换,替换为version:
、空格、 "
、 VERSION
变量内容和"
。 You may slightly shorten the command by using您可以通过使用稍微缩短命令
sed -i "" 's|\(version: "\)[[:digit:]]\(\.[[:digit:]]\)\{2\}"|\1'"${VERSION}"'"|' file.yaml
where \\(version: "\\)
is captured into Group 1 and, inside RHS, \\1
is used to insert it back and the repetition of .
and digit is done with another group quantified with \\{2\\}
(two times) quantifier.其中
\\(version: "\\)
被捕获到 Group 1 中,并且在 RHS 内部, \\1
用于将其插入回来,并且.
和 digit 的重复是用另一个用\\{2\\}
(两次)量词量化的组完成的.
sed -i -r "s@version: [0-9]+\.[0-9]+\.[0-9]+@version: ${VERSION}@" file.yaml
$ sed 's/\(version: "\)[^"]*/\1'"$VERSION"'/' file
version: "z.z.z"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.