Question

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?

EXAMPLE:

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";
} */
Was it helpful?

Solution

This might work for you (GNU sed):

sed '/{/!b;:a;$!N;/}/!ba;/font-family:\s*"foobar";/s/.*/\/* & *\//' file

OTHER TIPS

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top