Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escaping forward slashes in sed command [duplicate]

Tags:

bash

shell

sed

With Bash and SED I'm trying to replace two strings in a js file with URL's.

The two urls that should be inserted is input params when I run the .sh script.

./deploy.sh https://hostname.com/a/index.html https://hostname2.com/test 

However to make this usable in my sed command I have to escape all forward slashes with: \\ ?

./deploy.sh https:\\/\\/hostname.com\\/a\\/index.html https:\\/\\/hostname2.com\\/test 

If they are escaped this SED command works on Mac OSX Sierra

APP_URL=$1 API_URL=$2  sed "s/tempAppUrl/$APP_URL/g;s/tempApiUrl/$API_URL/g" index.src.js > index.js 

Now I don't want to insert escaped urls as params, I want the script it self to escape the forward slashes.

This is what I've tried:

APP_URL=$1 API_URL=$2  ESC_APP_URL=(${APP_URL//\//'\\/'}) ESC_API_URL=(${API_URL//\//'\\/'})  echo 'Escaped URLS' echo $ESC_APP_URL #Echos result: https:\\/\\/hostname.com\\/a\\/index.html  echo $ESC_API_URL #Echos result: https:\\/\\/hostname2.com\\/test  echo "Inserting app-URL and api-URL before dist" sed "s/tempAppUrl/$ESC_APP_URL/g;s/tempApiUrl/$ESC_API_URL/g" index.src.js > index.js 

The params looks the same but in this case the SED throws a error

sed: 1: "s/tempAppUrl/https:\\/\ ...": bad flag in substitute command: '\' 

Could anyone tell me the difference here? The Strings looks the same but gives different results.

like image 439
Jonathan Andersson Avatar asked Nov 21 '16 07:11

Jonathan Andersson


People also ask

How do you escape a forward slash?

In cases where you either cannot or prefer not to use alternate delimiters, you can escape the forward slashes with a backslash: m/\/[^/]+$/ for example (using an alternate delimiter that could become m{/[^/]+$} , which may read more clearly).

How do you remove slash in sed?

Since the text we are dealing with contains / , we're using % as an alternative delimiter in the sed expressions. This first removes the string . git (possibly followed by a / ) from the end of each line of input before applying the same substitution as in the first sed command.

How do you escape special characters in sed?

Sed needs many characters to be escaped to get their special meaning. For example, if you escape a digit in the replacement string, it will turn in to a backreference. Remember, if you use a character other than / as delimiter, you need replace the slash in the expressions above wih the character you are using.

How do you escape a forward slash in Linux?

You need to escape the / as \/ . The escape ( \ ) preceding a character tells the shell to interpret that character literally. Show activity on this post. Escape it !


1 Answers

I suggest to replace

sed "s/regex/replace/" file 

with

sed "s|regex|replace|" file 

if your sed supports it. Then it is no longer necessary to escape the slashes.

The character directly after the s determines which character is the separator, which must appear three times in the s command.

like image 200
Cyrus Avatar answered Sep 27 '22 21:09

Cyrus