1

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";
} */

2 Answers 2

2

This might work for you (GNU sed):

sed '/{/!b;:a;$!N;/}/!ba;/font-family:\s*"foobar";/s/.*/\/* & *\//' file
Sign up to request clarification or add additional context in comments.

1 Comment

+1, the idea is smart. Recommend to change the replace command by s!.*!/* & */!', when the replace chars with /
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.