user1770589
user1770589

Reputation: 375

How to escape colon and double slash in shell script?

I am writing shell script to replace a line a file which has an URI.I am getting error in using URI.

Line to be placed:

   source 'http://rubygems.org'

My script:

   #sed -i '1s/.*/  source 'https://rubygems.org' /' /Gemfile

It is not able to identify single quote.

Error i am getting: "sed: -e expression #1, char 23: unknown option to `s'"

Upvotes: 1

Views: 2909

Answers (3)

Adam T
Adam T

Reputation: 675

You can use alternate delimeters in your sed command.

For example, years ago I had a sed line that had to swap URLs and I used the @ at sign.

For an example, create a text file 'file.txt' with the following: I have a unix operating system from California

Run the following in your shell, using the @ instead of the usual slash delimiter.

$ sed 's@unix@linux@' file.txt
I have a linux operating system from California

Upvotes: 2

chaos
chaos

Reputation: 9302

sed accepts different separators:

sed -i '1s|.*|  source "https://rubygems.org" |' /Gemfile

And within single quotes you can also use double quotes.

Upvotes: 0

Etan Reisner
Etan Reisner

Reputation: 81022

The simplest solution is to use something like this instead.

sed -i "1s,.*,  source 'https://rubygems.org' ," /Gemfile

Double quotes around the sed script to allow internal single quotes and s,,, instead of s/// (since you can use just about any single character for the delimiter that you want).

Upvotes: 1

Related Questions