Geuis
Geuis

Reputation: 42267

Regex in sed to replace substrings

I'm having some trouble getting the right output here.

I have this sample text

test {
  abc: rem(1px);
  def: rem(2px) rem(3px);
}

And this current sed command

sed -lE "s/rem\((.*)\)/\1/g" test.css

But my output looks like this

test {
  abc: 1px;
  def: 2px) rem(3px;
}

And my goal is this

test {
  abc: 1px;
  def: 2px 3px;
}

Upvotes: 1

Views: 39

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626689

Use

sed -lE "s/rem\(([^()]*)\)/\1/g" test.css

to remove all individual rem()s. The [^()] bracket expression (if we use POSIX terminology) matches any character but ( and ).

Pattern details:

  • rem\( - match a literal rem(
  • ([^()]*) - Group 1 matching 0 or more characters other than ( and )
  • \) - a literal ).

The \1 restores the captured text in the resulting string after replacement.

Upvotes: 3

Related Questions