Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sed with special characters

Tags:

linux

bash

unix

sed

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 and replace all this line with:

ASD = $dsadad ( .cpp ) 

How can I make sed ignore special charactars, I tried adding back slash before special characters, but maybe I got it wrong, can some one show me an example?

Here is what i want :

sed 's/CPPS = \$(shell ls | grep \*\.cpp )/somereplace/' Makefile
like image 253
shd Avatar asked May 12 '11 15:05

shd


People also ask

How do you pass special characters in sed?

\ followed by a letter has a special meaning (special characters) in some implementations, and \ followed by some other character means \c or c depending on the implementation. With single quotes around the argument ( sed 's/…/…/' ), use '\'' to put a single quote in the replacement text.

How do you denote special characters in regex?

Special Regex Characters: These characters have special meaning in regex (to be discussed below): . , + , * , ? , ^ , $ , ( , ) , [ , ] , { , } , | , \ . Escape Sequences (\char): To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \.

How do you escape a dot in sed?

The reason is that a dot is a special character in regular expressions. If you mean an actual dot, you need to escape it with \.


1 Answers

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.

like image 134
Cédric Julien Avatar answered Oct 01 '22 19:10

Cédric Julien