Sed with special characters

I have this line that I want to use sed:

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

where $ start is not varaiable, I want to use sed on it and replace all this line:

 ASD = $dsadad ( .cpp ) 

How can I make sed ignore special characters, I tried adding a slash before special characters, but maybe I realized that this is not the case, can someone show me an example?

here is what i want:

sed 's / CPPS = \ $ (shell ls | grep * .cpp) / somereplace /' Makefile

+6
source share
3 answers
 sed 's/\$start/\$dsadad/g' your_file >> ASD = $dsadad ( *.cpp ) sed 's/\*//g' your_file >> ASD = $start ( .cpp ) 

To perform your editing:

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

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

+4
source

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

If you are in a shell, you may need to double escape $ , as this is a special character for both the shell (variable extension) and sed (end of line)

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

Do not run away ( or ) ; which actually makes them special (groups) in sed. Some other common characters include [ ] \ . ?

Here's how to avoid your example:

 sed 's/ASD = \$start ( \*\.cpp )/ASD = $dsadad ( .cpp )/' somefile 
+3
source

Symbols $ , * ,. are special for regular expressions, so they must be avoided so that they can be taken literally.

 sed 's/ASD = \$start ( \*\.cpp )/ASD = \$dsadad ( .cpp )/' somefile 
+1
source

Source: https://habr.com/ru/post/887946/


All Articles