Search code examples
stringsedtrim

sed remove specific key with varying value from string


Say I have a string:

ap=test:::bc=exam:::dc=comic:::mp=calc:::

Read in a linux box, i need to remove say bc=exam, the key is always the same, but the value can be any value, string or digits, and the placement of the key value pair can be anywhere in the string.

i've got to

sed -e 's/:::bc=\(.*:::\)*/\1/'

which only removes the key and a delimiter.

or

sed -e 's/:::bc=.*\(:::\)*/\1/'

which is removing everything from the key on.

Thanks in advance.


Solution

  • Since your values do not contain semicolons, you may match them with a negated bracket expression, [^:]*:

    sed  's/:::bc=[^:]*//' file
    

    See the online sed demo.

    The :::bc=[^:]* matches :::bc and then any 0+ chars other than a colon.