Tushar Sharma
Tushar Sharma

Reputation: 2882

Replace \ with white space

I have .sh file in which one of the variable is admFilter=${10}, i am receiving value for this variable from .bat file call, that's good it's working fine. When i do echo "$admFilter" i see my output-: [Name]="Account\List\View" (It's complete one value)

What i actually want is to remove "\" this and replace with white space, so final output will be [Name]="Account List View"' , and then store result in another variable, i am not getting how can i use SED command on this(Never used it before), i also tried the tr command but that combines the string giving [Name]="AccountListView"'. Can anyone help me how can i get desired output, and explain little on sed if possible? Thanks

OS VERSION -: SunOS 5.10 Generic_150400-52 sun4v sparc SUNW,T5240

Upvotes: 0

Views: 76

Answers (2)

randomir
randomir

Reputation: 18697

As an alternative to bash "search and replace" parameter expansion (explained in the other answer), you can also use tr:

echo '[Name]="Account\List\View"' | tr '\\' ' '
[Name]="Account List View"

or sed (with the s command):

echo '[Name]="Account\List\View"' | sed 's/\\/ /g'
[Name]="Account List View"

In both cases, just be sure to escape the backslash (with backslash): \\.

Additionally, to store it in another variable, y, starting with value in x:

x='[Name]="Account\List\View"'
y=$(tr '\\' ' ' <<<"$x")

Upvotes: 1

Nahuel Fouilleul
Nahuel Fouilleul

Reputation: 19335

assuming str contains [Name]="Account\List\View", following variable expansion replaces all \ with spaces:

str='[Name]="Account\List\View"'
echo "${str//\\/ }"

from bash manual

${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. 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.

Upvotes: 2

Related Questions