sed with special characters

前端 未结 3 2005
野性不改
野性不改 2020-12-20 17:00

I have this line that I want to use sed on:

--> ASD = $start ( *.cpp ) <--

where $start is not a varaiable, I want to use sed on it a

相关标签:
3条回答
  • 2020-12-20 17:46

    The chacters $,*,. are special for regular expressions, so they need to be escaped to be taken literally.

    sed 's/ASD = \$start ( \*\.cpp )/ASD = \$dsadad ( .cpp )/' somefile
    
    0 讨论(0)
  • 2020-12-20 17:54

    Backslash works fine. echo '*.cpp' | sed 's/\*//' => .cpp

    If you're in a shell, you might need to double escape $, since it's a special character both for the shell (variable expansion) and for sed (end of line)

    echo '$.cpp' | sed "s/\\$//" or echo '$.cpp' | sed 's/\$//' => '.cpp'

    Do not escape ( or ); that will actually make them them special (groups) in sed. Some other common characters include [ ] \ . ?

    This is how to escape your example:

    sed 's/ASD = \$start ( \*\.cpp )/ASD = $dsadad ( .cpp )/' somefile
    
    0 讨论(0)
  • 2020-12-20 18:00
    sed 's/\$start/\$dsadad/g' your_file
    >> ASD = $dsadad ( *.cpp ) 
    
    sed 's/\*//g' your_file
    >> ASD = $start ( .cpp ) 
    

    To follow your edit :

    sed -i 's/ASD = \$start ( \*.cpp )/ASD = \$dsadad ( .cpp )/' somefile
    >> ASD = $dsadad ( .cpp )
    

    Add the -i (--inplace) to edit the input file.

    0 讨论(0)
提交回复
热议问题