Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Trying to change the first background-color following the "body {" style, from whatever it is to "background-color: #000000;" in seven different stylesheets.css files. Also each .css file contains more than 10 instances, which is why I want to only change the one following the "body {" style code. Possible?

share|improve this question
add comment

2 Answers

Do some change according to your file:

sed  '0,/body{background-color:.*;/s//body{background-color:#000000;/' sample.css

or do it quickly:

sed  '0,/body{background-color:.*;/s//body{background-color:#000000;/' *.css    

sed ':a;N;s/\n/\a/;$!ba;s/body[ \t\a]*{[ \t\a]*background-color:#[0-9a-zA-Z]*;/body\n{background-color:#000000;/;s/\a/\n/g;' *.css

Source:The sed FAQ

share|improve this answer
    
Might fail if there is any text (or even newlines) between body{ and background-color. –  Andy Friese Dec 16 '12 at 9:15
    
+1 for creating that beast ;) –  Andy Friese Dec 16 '12 at 13:44
add comment

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...

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.