用 sed 替换子字符串 [英] Replace substring with sed

查看:49
本文介绍了用 sed 替换子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的字符串:

I have a string like this :

test:blabla

对于 sed,我想用其他东西替换 ':' 之后的内容.

And with sed, I want to replace what's after the ':' with something else.

我可以设法替换一个单词,但不能替换':'之后的那个.我在互联网上搜索了答案,但没有找到任何东西.

I can manage to replace a word, but not the one after the ':'. I searched on the internet for the answer, but I didn't found anything.

有什么帮助吗?

推荐答案

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

$ echo test:blabla | sed 's/:.*/:replaceword/'
test:replaceword

或者对于 test test:blabla test 的情况,您只想替换 : 后面的单词,请使用 sed 's/:[^ ]*/:replaceword/':

Or for the situation test test:blabla test where you only want to replace the word following : use 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

这篇关于用 sed 替换子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆