Replace substring with sed

后端 未结 2 889
醉梦人生
醉梦人生 2020-12-06 03:29

I have a string like this :

test:blabla

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

I can manage t

相关标签:
2条回答
  • 2020-12-06 03:43

    Use: sed 's/:.*/:replaceword/'

    $ echo test:blabla | sed 's/:.*/:replaceword/'
    test: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
    
    0 讨论(0)
  • 2020-12-06 03:53
    > echo $SER2
    test:blabla
    > echo $SER2 | sed 's/\([^:]*:\).*/\1replace/g'
    test:replace
    >
    
    0 讨论(0)
提交回复
热议问题