remove substring from a string with special characters using sed

Scenario 1:

string1=hello_how_are_you
string2=hello_ho
echo $string1 | sed -e "s/${string2}//g"
output : w_are_you

Scenario 2:

rule='\"create\":false,\"name\":\"specified\",\"queue\":null,\"rules\":null'
placement_rule='{\"create\":false,\"name\":\"specified\",\"queue\":null,\"rules\":null},{\"create\":false,\"name\":\"primaryGroup\",\"queue\":null,\"rules\":null},{\"create\":null,\"name\":\"secondaryGroupExistingQueue\",\"queue\":null,\"rules\":null},{\"create\":null,\"name\":\"default\",\"queue\":null,\"rules\":null}'

echo $placement_rule |  sed -e "s/${rule}//g"

output : {\"create\":false,\"name\":\"specified\",\"queue\":null,\"rules\":null},{\"create\":false,\"name\":\"primaryGroup\",\"queue\":null,\"rules\":null},{\"create\":null,\"name\":\"secondaryGroupExistingQueue\",\"queue\":null,\"rules\":null},{\"create\":null,\"name\":\"default\",\"queue\":null,\"rules\":null}

As you can see in scenario2 no change in output, what can be done to replace /remove strings like these using sed.

Expected output should have been:

{},{\"create\":false,\"name\":\"primaryGroup\",\"queue\":null,\"rules\":null},{\"create\":null,\"name\":\"secondaryGroupExistingQueue\",\"queue\":null,\"rules\":null},{\"create\":null,\"name\":\"default\",\"queue\":null,\"rules\":null}

Thanks

2 answers

  • answered 2019-03-14 02:58 tshiono

    Please try the following:

    rule='\"create\":false,\"name\":\"specified\",\"queue\":null,\"rules\":null'
    rule="$(echo "$rule" | sed -e 's/\\/\\\\\\/g')"    # This line escapes the backslashes
    placement_rule='{\"create\":false,\"name\":\"specified\",\"queue\":null,\"rules\":null},{\"create\":false,\"name\":\"primaryGroup\",\"queue\":null,\"rules\":null},{\"create\":null,\"name\":\"secondaryGroupExistingQueue\",\"queue\":null,\"rules\":null},{\"create\":null,\"name\":\"default\",\"queue\":null,\"rules\":null}'
    
    echo $placement_rule |  sed -e "s/${rule}//g"
    

    Output:

    {},{\"create\":false,\"name\":\"primaryGroup\",\"queue\":null,\"rules\":null},{\"create\":null,\"name\":\"secondaryGroupExistingQueue\",\"queue\":null,\"rules\":null},{\"create\":null,\"name\":\"default\",\"queue\":null,\"rules\":null}
    
    • The variable $rule needs to have escape sequences as \\\" which is then interpreted as \" in the sed replacement.
    • The added conversion s/\\/\\\\\\/g replaces single backslash with three sequential backslashes.

  • answered 2019-03-14 10:08 agc

    If the job is just to empty the first set of curly braces, it would be simpler not to use a complex ${rule} variable, and just clear out those braces:

    sed 's/{[^}]*}/{}/' <<< $placement_rule