[英]SED: replace semvers of multiple files
[ context ] My script needs to replace semvers of multiple .car
names with commit sha . [上下文] 我的脚本需要用commit sha替换多个.car
名称的semvers 。 In short, I would like that every dev_CA_1.0.0.car
became dev_CA_6a8zt5d832.car
简而言之,我希望每个dev_CA_1.0.0.car
都变成dev_CA_6a8zt5d832.car
ADDING commit sha right before .car
was pretty trivial.在.car
之前添加提交 sha非常简单。 With this, I end up with dev_CA_1.0.0_6a8zt5d832.car
有了这个,我最终得到了dev_CA_1.0.0_6a8zt5d832.car
find . -depth -name "*.car" -exec sh -c 'f="{}"; \
mv -- "$f" $(echo $f | sed -e 's/.car/_${CI_COMMIT_SHORT_SHA}.car/g')' \;
But I find it incredibly difficult to REPLACE .但我发现更换起来非常困难。 What aspect of sed
am I misconceiving trying this:我误解了sed
的哪个方面:
find . -depth -name "*.car" -exec sh -c 'f="{}"; \
mv -- "$f" $(echo $f | sed -r -E 's/[0-9\.]+.car/${CI_COMMIT_SHORT_SHA}.car/g')
or this或这个
find . -depth -name "*.car" -exec sh -c 'f="{}"; \
mv -- "$f" $(echo $f | sed -r -E 's/^(.*_)[0-9\.]+\.car/\1${CI_COMMIT_SHORT_SHA}\.car/g')' \;
no matches found: f="{}"; mv -- "$f" $(echo $f | sed -r -E ^(.*_)[0-9.]+.car/1684981321531.car/g)
or multiple variants:或多种变体:
\
escaping (eg \\.
) \
escaping(例如\\.
)(
and )
escaping (eg \(
) (I read somewhere that regex grouping with sed
requires some care with ()
) (
和)
escaping(例如\(
)(我在某处读到正则表达式与sed
分组需要注意()
)Is there a more direct way to do it?有没有更直接的方法呢?
$f
getting in sed
are path looking like $f
进入sed
的路径看起来像
./somewhere/some_project_CA_1.2.3.car
./somewhere_else/other_project_CE_9.2.3.car
You may use您可以使用
sed "s/_[0-9.]\{1,\}\.car$/_${CI_COMMIT_SHORT_SHA}.car/g"
See the online demo查看在线演示
Here, sed
is used with a POSIX ERE expression, that matches在这里, sed
与 POSIX ERE 表达式一起使用,它匹配
_
- an underscore _
- 下划线[0-9.]\{1,\}
- 1 or more digits or dots [0-9.]\{1,\}
- 1 个或多个数字或点\.car
- .car
(note that a literal .
must be escaped! a .
pattern matches any char) \.car
- .car
(注意文字.
必须转义!a .
模式匹配任何字符)$
- end of string. $
- 字符串结束。Can you try this:你可以试试这个:
export CI_COMMIT_SHORT_SHA=6a8zt5d832
find . -depth -name "*.car" -exec sh -c \
'f="{}"; echo mv "$f" "${f%_*}_${CI_COMMIT_SHORT_SHA}.car"' \;
Remove echo
once you are satisfied of the result.一旦您对结果感到满意,请删除echo
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.