gpt4 book ai didi

shell - 用 sed 替换子字符串

转载 作者:行者123 更新时间:2023-12-03 09:48:04 24 4
gpt4 key购买 nike

我有一个这样的字符串:

test:blabla

使用 sed,我想用其他东西替换 ':' 之后的内容。

我可以设法替换一个词,但不能替换“:”之后的词。我在互联网上搜索了答案,但我什么也没找到。

有什么帮助吗?

最佳答案

使用:sed 's/:.*/:replaceword/'

$ 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

关于shell - 用 sed 替换子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14194702/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com