I’ve got hundreds (more than 700) sets of web folders, each containing discrete CSS stylesheets. (If you are curious, they are online courses.)
Recently a decision was made that links should have underlines. I know that W3C decided that a long time ago, but this is a University and they like to re-decide things.
I’ve been trying to update all the CSS files using a RegEx search and replace.
Major hurdles so far have been:
- Windows. I don’t like it, I’m not using it. Command-line utilities like FART are great for single-line stuff, but writing a more customized and powerful search proved to be too much for it.
-
Multi-Line. CSS files are usually structured like this:
a, .surveypopup{ text-decoration:none; cursor:pointer; }Which means that the selector (the part before the “{“) is always on a separate line from the goodies. I want to match all selectors that modify “a” without an event (like :hover) and ensure that anything with “text-decoration:none” becomes “text-decoration:underline” without messing up any other styling code that may be sandwiched between.
- Case-insensitive. For RegEx, this shouldn’t be a problem. The authors of this CSS may or may not have gotten creative with their capitalization.
The command-line I’m currently erroring with is this:
find . -iname "*.css" | xargs sed -i "" "s|\(\ba\(,\|\.\|\s\|\b\)\[^\{\]\*\{\[^\}\]\*\)text-decoration\:none|a.\1text-decoration:underline;|g"
Which produces:
sed: 1: "s|\(\ba\(,\|\.\|\s\|\b\ ...": RE error: invalid repetition count(s)
I’m wondering if my needs justify writing a bash script? It would be nice to create a backup of each file if a modification is required. Multiple operations like that would be easier in a script…
Either way, I assume I’m having problems because I don’t know what to escape for sed, and what not to escape.
Please help!
Operating on an entire file at once you can use:
More nicely formatted, this is:
Example file:
And result:
You can use perl’s
-iflag (don’t forget to set a backup extension!) to operate on the files in-place.There’s obviously a lot of other possible CSS rules which can include an
a; e.g.html>aordiv a b; this regex will not find the first, and will find the second, but will be “wrong” in both cases. Basically, you can use regex for these types of tasks only when you can make strong assumptions about the text you’re manipulating.update added
}to part of a rule to avoid matching, e.g.: