karezza
karezza

Reputation: 606

sed replace with unknown section in css

I could use an assist with a sed search and replace.

In this situation the file is css and the section I want to replace is among other values being set in the css. The following works:

sed -i 's/PlayButton-playCircle-kffp_v{border:2px solid hsla(0,0%,100%,.7);border-radius:50%;color:hsla(0,0%,100%,.7);display:inline-block/PlayButton-playCircle-kffp_v{border:2px solid hsla(0,0%,100%,.7);border-radius:50%;color:hsla(0,0%,100%,.7);display:none/g' file.css

But in another file.css where I wish to do the same thing the values surrounding the 'display:inline-block' may be in a different order. So it would make more sense to use something like a '.*' like "PlayButton-playCircle-kffp_v{.*;display:inline-block".

The css file is being generated automatically, once generated it is fixed, but I can't be sure what order the attributes are within the {}, I need to match the item with PlayButton-playCircle-...... that has the attribute display:inline-block and change to display:none.


Updating with answer formed with information from both commenters:

sed -i 's/\(PlayButton-playCircle-......{[^}]*;\)display:inline-block/\1display:none/g' $maincss

Instead of .*, [^}]* is used to match in the same way as .* but not matching '}' if found. This ensures the match is kept to one level of depth within {}.

Thank you!

Upvotes: 0

Views: 43

Answers (1)

Itagaki Fumihiko
Itagaki Fumihiko

Reputation: 564

You can rewrite it a bit as follows.

sed -i 's/\(PlayButton-playCircle-kffp_v{.*\)display:inline-block/\1display:none/g' file.css

The text between \( and \) will be captured and can be referenced as \1 in the replacement string.

Upvotes: 1

Related Questions