Ajarudin Gunga
Ajarudin Gunga

Reputation: 453

How to perform SED command with Special character like && and '/' for search and Replace?

I have search string like && !this.peekStartsWith('//') and want to replace with blank space . I have tried

sed -i 's/&& !this.peekStartsWith('\/\/')/ /g' dist/vendor.bundle.js

but, getting an error like

bash: !this.peekStartsWith: event not found

Can anyone help to work around this?

Upvotes: 0

Views: 990

Answers (2)

Jotne
Jotne

Reputation: 41460

I do only get this to work when escaping the !

sed "s|&& \!this\.peekStartsWith('//')| |g" file

Example file

cat file
some data&& !this.peekStartsWith('//')more data

test

sed "s|&& \!this\.peekStartsWith('//')||g" file
some datamore data
sed "s|&& \!this\.peekStartsWith('//')| |g" file
some data more data
sed "s|&& \!this\.peekStartsWith('//')|***|g" file
some data***more data

Upvotes: 1

RavinderSingh13
RavinderSingh13

Reputation: 133770

I could reproduce the error. You need to set your history off as follows first, since you have ! in your text which you are placing inside sed command, is causing this error.

set +o history

Now When you run sed command as follows, it works well.

echo "test && !this.peekStartsWith('//') bla" | sed "s|&& !this\.peekStartsWith('//')||g"
test  bla

To start history again you could use:

set -o history

Upvotes: 4

Related Questions