replace all '/' with '\/' in bash/shell script

jquery string replace all
javascript replace all commas
javascript replace all spaces
javascript replace all instances
replace all es6
javascript replace all variable
replace all dots in string javascript
lodash replace all

This might be very simple but I am stuck at getting this done in shell/bash. I have input like "a/b/c/d" and I want to replace all '/' with '\/' and the output would be 'a/b/c/d'

another example is 12.12.12.12/32 >>> 12.12.12.12\/32

Any help would be appreciated.

You can use bash's parameter expansion:

$ input="a/b/c/d"
$ echo "${input//\//\\/}"
a\/b\/c\/d

Given a variable input, ${input//xxx/yyy} yields the content of input with every xxx replaced with yyy. Here xxx is \/ (/ must be escaped to not be mixed up with the next /) and yyy is \\/ (\/ with \ being escaped).

How to replace all occurrences of a string?, Note: Don't use this in performance critical code. As an alternative to regular expressions for a simple literal string, you could use str = "Test abc test test abc test. The java string replaceAll () method returns a string replacing all the sequence of characters matching regex and replacement string. Let's see an example to replace all the occurrences of a single character. Let's see an example to replace all the occurrences of single word or set of words. String s1="My name is Khan.

Using bash only

var='a/b/c/d'
echo "${var//\//\\/}"

String.prototype.replaceAll · V8, JavaScript now has first-class support for global substring replacement through the new `String.prototype.replaceAll` API. Description: Replace each target element with the set of matched elements. version added: 1.2.replaceAll( target ) target. Type: Selector or jQuery or Array or Element. A selector string, jQuery object, DOM element, or array of elements indicating which element(s) to replace.

This would do:

echo "12.12.12.12/32" |sed 's,/,\\/,g'

Output:

12.12.12.12\/32

In such scenarios, use a delimiter other than /, as i used , in the above example. You can use for example: #,?....etc

3 Ways To Replace All String Occurrences in JavaScript, You can replace all occurrences of a string using split and join approach, replace​() with a regular expression and the new replaceAll() string  public String replaceAll(String regex, String replacement) Parameters. Here is the detail of parameters − regex − the regular expression to which this string is to be matched. replacement − the string which would replace found expression. Return Value. This method returns the resulting String. Example

Depending on how you access the text or strings you want to make the replacement in, something like this might work. You have to escape both / and \ inside the sed command.

var='a/b/c/d'
echo "$var" | sed 's/\//\\\//g'

Java String replaceAll() method, The java string replaceAll() method returns a string replacing all the sequence of characters matching regex and replacement string. Internal implementation. The.replace() method can actually accommodate replacing all occurrences; however, the search string must be replaced with a regular expression. A regular expression — regex for short — is an

String.prototype.replace(), The replace() method returns a new string with some or all matches of a pattern replaced by a replacement. The difference between replace() and replaceAll() method is that the replace() method replaces all the occurrences of old char with new char while replaceAll() method replaces all the occurrences of old string with the new string. Basically replace() works with replacing chars and replaceAll() works with replacing part of strings.

How to replace all occurrences of a string in JavaScript, Replacing First Match Only: If we specify the first argument as string, the replace function only replaces the first occurrence of the string. Consider  In this article we will discuss how to replace all occurrences of a sub string with new string in C++. For example, we have a string i.e. And we want replace all occurrences of ‘Lib’ with XXX. Let’s see the different methods to do it, If you didn't find what you were looking, then do suggest us in the comments below.

How To Replace All Occurrences Of A Word In A JavaScript String , replaceAll(String regex, String replacement) to replace all occurrences of a substring (matching argument regex ) with replacement string. 1.1. Method syntax​  To replace text or numbers, press Ctrl+H, or go to Home > Find & Select > Replace. In the Find what box, type the text or numbers you want to find. In the Replace with box, enter the text or numbers you want to use to replace the search text. Click Replace or Replace All.

Comments
  • Currently, your question is not legible or comprehensible. Please use the formatting tools to show raw characters. For starters, you could put the example parts between backticks `...`
  • I've edited the question to make backslashes visible (as @chryss suggested) -- if this is not what's intended, please revert the edit.
  • Why do you want to do this? It's not to make it acceptable to sed is it?
  • exactly, I need it just to make 'sed' acceptable...is there any other way around then probably I do not need replace at all?
  • Yes, use another delimiter, e.g. sed "s#$foo#$bar#g"
  • echo "$var", not echo $var; see BashPitfalls #14. If the user's input were * / * / 3 / 4, leaving the expansion unquoted would result in output containing a list of files.
  • OK, fair enough, for style reasons. I've made the change. However, I don't for a second expect that part to be relevant to the OP's script - I just added it to have something to hang the sed command on, and given I provided the input, I control it.
  • If we're a teaching resource, it's important to demonstrate practices that are safe to apply broadly. Part of why the ABS is considered such a poor resource in the bash community isn't so much outright inaccuracies (though there are a few) as examples that showcase unsafe practices; those practices tend to get copied and applied, even when their presence is only incidental, leading to people who learned from the ABS writing bad code.