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'
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
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
(...) 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
- 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.
/g at the end makes
sed repeat the substitution all non-overlapping matches in the data.
-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
Your command would therefor also be written portably as