Skip to content

Instantly share code, notes, and snippets.

@markbrown4
Last active March 16, 2018 17:34
Show Gist options
  • Save markbrown4/3b993d29aa5ed3980594c0f62cd87d50 to your computer and use it in GitHub Desktop.
Save markbrown4/3b993d29aa5ed3980594c0f62cd87d50 to your computer and use it in GitHub Desktop.

I'm trying to write a program that replaces a scss function call and doubles the input e.g.

input

.color {
  margin: spacing(1.5);
}

.test {
  margin: spacing(2) spacing(3);
}

output

.color {
  margin: spacing(3);
}

.test {
  margin: spacing(4) spacing(6);
}

This almost works

gawk '{
  line = $0
  if (match(line, /spacing\(/)) {
    print gensub(/spacing\(([0-9\.]+)\)/, "spacing(\\1 * 2)", "g", line)
  } else {
    print line
  }
}' ./test.scss > ./test2.scss
// test2.scss

.color {
  margin: spacing(1.5 * 2);
}

.test {
  margin: spacing(2 * 2) spacing(3 * 2);
}
  • Can you multiply by two rather than appending " * 2" to the match?
  • How can you run on all **/*.scss files and replace them?
@markbrown4
Copy link
Author

markbrown4 commented Mar 16, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment