Hey I've been having struggles trying to decipher what a specific sed command would do despite my research I seem to be stumped.
sed -E 's/([0-9]+)-([0-9]+)/\2:\1/g'
Hey I've been having struggles trying to decipher what a specific sed command would do despite my research I seem to be stumped.
sed -E 's/([0-9]+)-([0-9]+)/\2:\1/g'
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.
The non-standard -E
option to sed
makes the utility use extended regular expressions (ERE) rather than basic regular expressions (BRE), which is the standard for sed
. In an ERE, capturing groups are written (...)
, while a BRE uses \(...\)
. A BRE also does not have +
to specify a non-empty sequence of matches of an expression, but would use \{1,\}
instead.
Your command would therefor also be written portably as
sed 's/\([0-9]\{1,\}\)-\([0-9]\{1,\}\)/\2:\1/g'
or as
sed 's/\([0-9][0-9]*\)-\([0-9][0-9]*\)/\2:\1/g'