Reputation: 8938
I have been searching for a way to comment out a .css
file that may have a certain font-family indicated in the @font-face
, #id
, selector and/or .class
. I can do this with sed
if everything is on one line with /foobar/ a\
(append) and /foobar/ i\
(insert) but what is the best method to do this if everything is on multiple lines?
Before
@font-face {
font-family: "foobar";
src: url(fonts/foobar.ttf);
}
h1 {
font-family: "foobar";
}
.foobar {
font-family: "foobar";
}
End result:
/* @font-face {
font-family: "foobar";
src: url(fonts/foobar.ttf);
} */
/* h1 {
font-family: "foobar";
} */
/* .foobar {
font-family: "foobar";
} */
Upvotes: 1
Views: 129
Reputation: 58473
This might work for you (GNU sed):
sed '/{/!b;:a;$!N;/}/!ba;/font-family:\s*"foobar";/s/.*/\/* & *\//' file
Upvotes: 2
Reputation: 247012
Can do it with perl and awk
perl -00 -lpe '$_ = "/* $_ */" if /font-family:\s+.foobar/' file
awk -v RS= '/font-family:[[:blank:]]+.foobar/ {$0 = "/* " $0 " */"} 1' file
Both of these work the same way: read the file paragraph-by-paragraph (blank line delimited), test the regular expression, and add the comment markers if found.
To edit the file inplace, you would use
perl -i ...(as above)...
awk ...(as above)... > tmpfile && mv tmpfile file
Upvotes: 1