Search and replace with sed when dots and underscores are present

前端 未结 5 1043
-上瘾入骨i
-上瘾入骨i 2020-12-03 04:51

How do I replace foo. with foo_ with sed simply running

sed \'s/foo./foo_/g\' file.php

doesn\'t work. Thanks!

5条回答
  •  独厮守ぢ
    2020-12-03 04:58

    Interestingly, if you want to search for and replace just the dot, you have to put the dot in a character set. Escaping just the dot alone in a sed command for some reason doesn't work. The dot is still expanded to a single character wild card.

    $ bash --version  # Ubuntu Lucid (10.04)
    

    GNU bash, version 4.1.5(1)-release (x86_64-pc-linux-gnu)

    $ echo aa.bb.cc | sed s/\./-/g  # replaces all characters with dash
    

    '--------'

    $ echo aa.bb.cc | sed s/[.]/-/g  # replaces the dots with a dash
    

    aa-bb-cc

    With the addition of leading characters in the search, the escape works.

    $ echo foo. | sed s/foo\./foo_/g  # works
    

    foo_

    Putting the dot in a character set of course also works.

    $ echo foo. | sed s/foo[.]/foo_/g  # also works
    

    foo_

    -- Paul

提交回复
热议问题