0

I have 12k lines of SCSS code and I need to replace all CSS transitions like this:

transition: background .3s ease;

or

-webkit-transition: background .3s ease;

with a SASS mixin

@include _transition(background .3s ease);

After lots of research I've end up with a sed command like this

sed -i '' 's/transition: ([a-zA-Z0-9.]*);/@include _transition\((\1)\);/' *

But of course it doesn't work. I'm using a Mac and the error I get is:

sed: 1: "s/transition: ([a-zA-Z0 ...": \1 not defined in the RE

Please help!

1 Answer 1

2

You need to add space inside the character class and you don't need to escape ( , ) in the replacement part. Normal sed uses BRE(Basic regular expression) so the capturing group must be like \(blah blah\) not like (blahblah)

sed 's/transition: \([a-zA-Z 0-9.]*\);/@include _transition(\1);/g' file

Example:

$ cat file
transition: background .3s ease;
foo
$ sed 's/transition: \([a-zA-Z 0-9.]*\);/@include _transition(\1);/g' file
@include _transition(background .3s ease);
foo
Sign up to request clarification or add additional context in comments.

3 Comments

I don't think it works for the second example. I get this: -webkit-@include _transition(background .3s ease);
@AvinashRaj You're awesome and your answer is awesome. It works.
@David You're right. I made it work that way that firstly I've run sed which replaced all -webkit prefixed transitions, then second one to replace not prefixed ones.

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.