[英]Sed: replace last dot
How to replace the last matching dot
? 如何替换最后一个匹配
dot
?
for example, I'd like to change test.jpg to test.th.jpg 例如,我想将test.jpg更改为test.th.jpg
what I've tried: 我尝试过的
echo "test.jpg" | sed 's@[^\.]*$@\.th.@g'
This should work: 这应该工作:
$ echo "test.jpg" | sed 's/\.\([^.]*\)$/.th.\1/'
It gives: 它给:
test.th.jpg
Explanation: 说明:
\. literal dot
\( start capturing group
[^.] any character except dot
* zero or more
\) close capturing group
$ end of line
In the replacement \\1
replaces the content of the first capturing group :) 在替换
\\1
替换第一个捕获组的内容:)
kent$ sed 's/[^.]*$/th.&/' <<<"test.jpg"
test.th.jpg
or 要么
kent$ sed 's/.*\./&th./' <<<"test.jpg"
test.th.jpg
or 要么
kent$ awk -F. -v OFS="." '$NF="th."$NF' <<< "test.jpg"
test.th.jpg
You can also use awk
, prepend "th." 您也可以使用
awk
,在“ th”之前加上前缀。 to the last field 到最后一个领域
$ awk 'BEGIN{FS=OFS="."}$NF="th."$NF' <<< "test.jpg"
test.th.jpg
Using pure bash 使用纯bash
$ str="test.abc.jpg"
$ echo ${str%.*}.th.jpg
test.abc.th.jpg
如果您的系统上有Ruby
echo test.jpg | ruby -e 'x=gets.split(".");x.insert(-2,"th"); puts x.join(".")'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.