1

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.

5
  • 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 `...` Commented Oct 4, 2018 at 0:40
  • I've edited the question to make backslashes visible (as @chryss suggested) -- if this is not what's intended, please revert the edit. Commented Oct 4, 2018 at 0:48
  • Why do you want to do this? It's not to make it acceptable to sed is it? Commented Oct 4, 2018 at 1:07
  • exactly, I need it just to make 'sed' acceptable...is there any other way around then probably I do not need replace at all? Commented Oct 4, 2018 at 2:11
  • 1
    Yes, use another delimiter, e.g. sed "s#$foo#$bar#g" Commented Oct 4, 2018 at 6:02

4 Answers 4

7

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).

Sign up to request clarification or add additional context in comments.

Comments

0

Using bash only

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

Comments

0

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

Comments

0

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'

3 Comments

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.