0

I'm trying to create a sed expression to add a new line before a media query, but if run the expression again then it should not add the new line twice.

Currently I can add a new line after a semicolon or after an opening bracket, but when I try to check that "@media" has to be after in order to add the new line then the expression do not work.

sed -e 's/\([;{]\)\(\|\)/\1\n\2/' styles.less

Also I can add a line before media query, preserve its indentation, but not able to detect if it is preceded by ";" or "{". I don't want duplicate new lines if the command is run twice.

sed -ie 's/\(\s*\)@media/\n\1@media/' estilos.less

Just in case, I'm using CSS comb (with ATOM), which does a lot of things for my LESS or SASS files, but does not create this newline before the media queries.

Thanks for your help.

2
  • 1
    Can you add examples of input and expected output? Commented Apr 15, 2018 at 15:21
  • sed is for doing s/old/new/, that is all. It's not the right tool for inserting lines before or after other lines and it's certainly not for anything more complicated like checking if a line already exists before inserting it before some regexp or string match. edit your question to include concise, testable sample input and expected output and you'll get a clear, simple awk answer. Commented Apr 15, 2018 at 15:33

2 Answers 2

1

Your question isn't clear but it sounds like what you're looking for is:

awk '/@media/ && (p!=""){print ""} {p=$0} 1' file

For example:

$ cat file
here are 3
        first:
        @media
            second:
            @media
    third (blank line already present):

    @media
occurences

$ awk '/@media/ && (p!=""){print ""} {p=$0} 1' file
here are 3
        first:

        @media
            second:

            @media
    third (blank line already present):

    @media
occurences

The above will work with any awk in any shell on any UNIX box.

Sign up to request clarification or add additional context in comments.

Comments

1

sed POSIX BRE doesn't support \s shorthand. You have to use corresponding character class [\r\n\t\f\v ] (without \n). If you are in need of a newline character before media queries (@media) that doesn't follow a linebreak (^[\r\t\f\v ]*$) you could do this:

sed -i '/^[\r\t\f\v ]*$/{N;/@media/!{P;D;};b;};/@media/i\

' file

Address matches blank lines then it reads next line to pattern space. If it doesn't match a @media it prints every thing up to first newline character from pattern space (P), deletes it (D) and passes control back to process remaining line in PS (restarts cycle). If it matches @media it jumps over all commands and prints whole pattern space.

If all above conditions weren't matched, on matching a @media a linebreak will be inserted before current line using i command.

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.