captain yossarian
captain yossarian

Reputation: 457

sed - replacing line with special characters

not quite sure how to get this working. apologies, i'm kinda new to this.

so i have a file with a line that starts with

    <script type="text/javascript">var NREUMQ=NREUMQ

I want to repace that line with

</head>

I can't seem to get it going. I understand I need something like this:

sed 's/^    <script type="text/javascript">var NREUMQ=NREUMQ/hello/g index3.html

but I am not sure where to escape the special characters. obviously the '/' needs it. can anyone lend a hand?

Upvotes: 1

Views: 1918

Answers (5)

NeronLeVelu
NeronLeVelu

Reputation: 10039

sed "/<script type=\"text\/javascript\">var NREUMQ=NREUMQ/ s#.*#</head>#"

You can also keep the space at the begining with s#^\( *\).*#\1</head>#"

Upvotes: 0

Cooler
Cooler

Reputation: 55

You can use:

sed 's|<script type="text/javascript">var NREUMQ=NREUMQ|</head>|'

Upvotes: 0

lurker
lurker

Reputation: 58324

You can escape just about anything using backslash:

sed "s/^    <script type=\"text\/javascript\">var NREUMQ=NREUMQ/hello/g" index3.html

The nice thing about sed is that you can also use alternate delimeters. It will use whatever you put after the s, which means you wouldn't have to escape a slash:

sed "s#^    <script type=\"text/javascript\">var NREUMQ=NREUMQ#hello#g" index3.html

Upvotes: 2

ensc
ensc

Reputation: 6994

you can escape them by backslash; but you can also write

sed 's!...!...!' 

(the '!' can be an arbitrary character)

Upvotes: 1

anubhava
anubhava

Reputation: 786359

Use alternate regex delimiter, other than / since your matching text has / in it:

sed -i.bak 's~^ *<script type="text/javascript">var NREUMQ=NREUMQ~</head>~' index3.html

Upvotes: 2

Related Questions