I have a line as below in a html file,

shahar.push(['remoteUrl', 'staging.zazzercode.com']);

Depending upon environment, I want to replace the remoteUrl value with sed command.


shahar.push(['remoteUrl', 'staging.zazzercode.com']);
shahar.push(['remoteUrl', 'production.zazzercode.com']);

I used

sed -i '/remoteUrl/c\shahar.push(['remoteUrl','staging.zazzercode.com']);' predict.html

It works but removing single quotes over 'remoteUrl' and 'staging.zazzercode.com' causing html error.

shahar.push([remoteUrl, staging.zazzercode.com]);

So to be safe, I want to change sed command just to change the value between two single quotes after , in a line with remoteUrl.

Or I want other sed options than What I knew to replace whole line but with single quotes.


The problem is that bash is processing your command line before sed gets a hold of it. In this case, the solutions is to change the outer quotes to double-quotes:

sed -i "/remoteUrl/c\shahar.push(['remoteUrl','staging.zazzercode.com']);" predict.html

The issue was that bash performs quote removal before passing the command to sed. In bash's eyes, your original command was a series of single-quoted strings and bash removed all the quotes. In the version above, bash sees a single double-quoted string. While bash removes those double-quotes before passing the string to sed, it leaves the inner single-quotes alone.

  • true, I fixed that :) – prayagupd Aug 4 '14 at 19:18

So, to be safe, I want to change the sed command just to change the value between two single quotes after a comma (,) in a line with 'remoteUrl'.

sed "s/\('remoteUrl',.*'\).*\('\)/\1staging.zazzercode.com\2/"

s is search, substitute, and replace.  \( and \) in the “old” string (specified first) identify substrings.  \1 and \2 (and \n, up to \9) copy those substrings into the replacement string.

  • Thx Scott, i'll check it later. – prayagupd Aug 4 '14 at 21:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.