Skip to main content
1 of 2
Kusalananda
  • 356.1k
  • 42
  • 737
  • 1.1k

Your sed expression is a substitution that swaps each pair of non-overlapping positive integers around a dash, and replaces the dash with a colon.

Your expression would turn

000-212 444-818 

into

212:000 818:444 

The (...) bits are "capturing groups". Such a group captures the substring that is matched by the expression within it. This substring is then available through \1 for the first group, \2 for the second, etc.

Your capturing groups both use [0-9]+, which is a pattern that matches a non-empty sequence of digits between 0 and 9, for example the string 123000.

The - between the two groups would match a literal dash in the data.

The replacement string makes use of the numbers matched by the groups, but uses them in the opposite order from how they were matched, swapping them. The replacement text also inserts a : in place of the - between the numbers.

The /g at the end makes sed repeat the substitution all non-overlapping matches in the data.

Kusalananda
  • 356.1k
  • 42
  • 737
  • 1.1k