我将会为您翻译以下内容:
我有一个这样的字符串:
test:blabla
我想使用sed命令将冒号后面的内容替换成其他内容。
我可以替换一个单词,但无法替换冒号后面的内容。我在互联网上搜索答案,但没有找到任何有用的信息。
请问有什么帮助吗?
使用:sed 's/:.*/:替换的单词/'
$ echo test:blabla | sed 's/:.*/:replaceword/'
test:replaceword
或者对于情况 test test:blabla test
,您只想替换紧接着 :
的单词,请使用 sed 's/:[^ ]*/:replaceword/'
:
$ echo "test test:blabla test" | sed 's/:[^ ]*/:replaceword/'
test test:replaceword test
# Use the g flag for multiple matches on a line
$ echo "test test:blabla test test:blah2" | sed 's/:[^ ]*/:replaceword/g'
test test:replaceword test test:replaceword
> echo $SER2
test:blabla
> echo $SER2 | sed 's/\([^:]*:\).*/\1replace/g'
test:replace
>