Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to replace "/" on path string with "\/" using sed?

Tags:

bash

shell

sh

sed

I've tried this:

PATH="/user/dj/a/mydir"
PATH_FORMAT=`echo "${PATH}" | sed 's/\//\\\//'`

but it only replace the first "/". I want the resulting PATH_FORMAT value to be:

"\/user\/dj\/a\/mydir"

How can I do that?

like image 413
qiushuitian Avatar asked Dec 20 '12 11:12

qiushuitian


People also ask

How do you replace a variable in a file using sed?

For replacing a variable value using sed, we first need to understand how sed works and how we can replace a simple string in any file using sed. In this syntax, you just need to provide the string you want to replace at the old string and then the new string in the inverted commas.

How can I replace a newline (\ n using sed?

The `sed` command can easily split on \n and replace the newline with any character. Another delimiter can be used in place of \n, but only when GNU sed is used. When the \n is missing in the last line of the file, GNU sed can avoid printing \n. Furthermore, \n is usually added to each consecutive output of `sed`.

Which sed command is used for replacement?

The s command is probably the most important in sed and has a lot of different options. Its basic concept is simple: the s command attempts to match the pattern space against the supplied regexp; if the match is successful, then that portion of the pattern space which was matched is replaced with replacement.


2 Answers

Add a g flag to your substitute command:

echo "${PATH}" | sed 's/\//\\//g'

Or more readable, as per Jonathan Wakely in the comments:

echo "${PATH}" | sed 's:/:\\/:g'

To achieve what you describe in your question, you need one more backslash:

echo /user/dj/a/mydir | sed 's:/:\\\/:g'

Output:

\/user\/dj\/a\/mydir
like image 61
Thor Avatar answered Nov 15 '22 07:11

Thor


Use Bash's parameter expansion, if you are running a shell script:

MYPATH="/user/dj/a/mydir"
PATH_FORMAT=${MYPATH////\\/}

The substition ${parameter} is extended by the syntax ${parameter//pattern/string} which replaces all patterns found in parameter. The pattern can be a regular expression. In your case, the pattern is: /. The replace string is: \\/.

Furthermore, avoid storing something in the variable PATH, because it is probably used by the system -- unless you want to modify the system's path variable.

Quoting gnu.org's manual on parameter expension:

${parameter/pattern/string}

The pattern is expanded to produce a pattern just as in filename expansion. Parameter is expanded and the longest match of pattern against its value is replaced with string. The match is performed according to the rules described below (see Pattern Matching). If pattern begins with /, all matches of pattern are replaced with string. Normally only the first match is replaced. If pattern begins with #, it must match at the beginning of the expanded value of parameter. If pattern begins with %, it must match at the end of the expanded value of parameter. If string is null, matches of pattern are deleted and the / following pattern may be omitted. If the nocasematch shell option (see the description of shopt in The Shopt Builtin) is enabled, the match is performed without regard to the case of alphabetic characters. If parameter is @ or *, the substitution operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with @ or *, the substitution operation is applied to each member of the array in turn, and the expansion is the resultant list.

like image 32
blubase Avatar answered Nov 15 '22 07:11

blubase