Generally you need a good regular expression matching your needs. sed
is a good tool to use, perl
is another. Only reason I'm preferring perl
is that I know its regular expression syntax better. The following line should do what you want:
perl -0777 -i.bak -pe 's/(body {.*?background-color.*?)#.{6}/$1#000000/si;' ./style.css
-0777
means split on nothing, so slurp in whole file (instead of line by line, the default for -p)
-i.bak
means alter files in place, leaving original with extension .bak (default for -p is to just print)
-p
means pass line by line (except we are slurping) to passed code (see -e)
-e
expects code to run.
Explaining the regular expression is beyond the scope of my answer, but there are many tutorials out there like this one for example.
Be sure to check the results as I can easily imagine css files which would cause problems with this expression. For example having selectors which contain the word body might cause problems...