How to replace to apostrophe ' inside a file using SED

后端 未结 4 1427
没有蜡笔的小新
没有蜡笔的小新 2020-12-15 21:46

I have a file \"test.txt\" that contain the following

+foo+
+bar+

What I want to do is to replace them into:

\'foo\'
\'bar\         


        
相关标签:
4条回答
  • 2020-12-15 22:29

    Use " instead. And add g flag to replace all.

    sed  "s/\+/\'/g" test.txt
    
    0 讨论(0)
  • 2020-12-15 22:49

    You can also replace all instances of + with ' in the file by using tr:

    tr '+' "'" < inputfile
    
    0 讨论(0)
  • 2020-12-15 22:51

    + is not a special character without -r switch in sed. You can run the substitute command without any escaping:

    echo '+foo+' | sed "s/+/'/g"
    
    # output: 'foo'
    

    If you want to save changed file then use:

    sed -i.bak "s/+/'/g" test.txt
    
    0 讨论(0)
  • 2020-12-15 22:52

    This might work for yoyu (GNU sed):

    sed 'y/+/'\''/' file
    
    0 讨论(0)
提交回复
热议问题