1

I have a following string in a shell script.

var="rule:access=allow,default=default value,OTHER=0,SOMETHING=1"

I need to remove "default=default value" from $var. Thus, the final string will become as follows:

var="rule:access=allow,OTHER=0,SOMETHING=1"

NOTE: The value after default may be anything. It could be "default=something else". It is not fixed. I need to remove that text. How can this be done?

3 Answers 3

3

Answer for revised question

$ echo "$var" | sed 's/default=[^,]*,//'
rule:access=allow,OTHER=0,SOMETHING=1

If we want to update the value of var, we use command substitution:

$ var="$(echo "$var" | sed 's/default=[^,]*,//')"
$ echo "$var"
rule:access=allow,OTHER=0,SOMETHING=1

Answer for original question

Using bash

To remove default=default value,, use pattern substitution:

$ var="${var/default=default value,/}"
$ echo "$var"
rule:access=allow,OTHER=0,SOMETHING=1

This feature is documented in man bash:

${parameter/pattern/string}
Pattern substitution. The pattern is expanded to produce a pattern just as in pathname 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 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.

Using sed

$ var="$(echo "$var" | sed 's/default=default value,//')"
$ echo "$var" 
rule:access=allow,OTHER=0,SOMETHING=1
Sign up to request clarification or add additional context in comments.

1 Comment

The value after default is not always fixed. That is it can be anything. It can be default=something else. I need to remove that.
0

Assign this to var again.

echo $var|sed 's/default=default value//'|sed 's/,,/,/'

Last sed to take care of scenarios where default=default value is the first or last keyword in string.

1 Comment

The value after default is not always fixed. That is it can be anything. It can be default=something else. I need to remove that
0

You can use two parameter expansions, one to get the stuff after the comma of default=* and one to get the part before default....

var="rule:access=allow,default=default value,OTHER=0,SOMETHING=1"

endo=${var#*default}
echo ${var/default=*}${endo#*,}

rule:access=allow,OTHER=0,SOMETHING=1

But sed might be easier...!

Comments

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.