Bug 242013 - sed(1): matching back references works only some of the time
Summary: sed(1): matching back references works only some of the time
Status: New
Alias: None
Product: Base System
Classification: Unclassified
Component: bin (show other bugs)
Version: 12.0-RELEASE
Hardware: Any Any
: --- Affects Some People
Assignee: freebsd-bugs (Nobody)
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-11-16 22:15 UTC by Keith Hellman
Modified: 2019-11-17 05:00 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Keith Hellman 2019-11-16 22:15:50 UTC
Matching back references according to re_format(7):
  
     Finally, there is one new type of atom,
     a back reference: ‘\’ followed by a non-zero decimal digit d matches the
     same sequence of characters matched by the dth parenthesized
     subexpression (numbering subexpressions by the positions of their opening
     parentheses, left to right), so that (e.g.)  ‘\([bc]\)\1’ matches ‘bb’ or
     ‘cc’ but not ‘bc’.

does not always seem to work.  For instance, I would expect all of these 
to print the single input line:

$ echo '#20#20' | sed -n -E -e '/(#|0x)(..)\1\2/p'
$ echo '#2020#' | sed -n -E -e '/(#|0x)(..)\2\1/p'
$ echo '#202020' | sed -n -E -e '/(#|0x)(..)20\2/p'
#202020
$ echo '#202020' | sed -n -E -e '/(#|0x)(..)\2(20)/p'
$ echo '#202020' | sed -n -E -e '/(#|0x)(..)(20)\2/p'
#202020
$ echo '#202020' | sed -n -E -e '/(#|0x)(..)..\2/p'
#202020
$ echo '#20#20' | sed -n -E -e '/(#|0x)(..).\2/p'
#20#20

Additionally, gsed(1) equivalent invocations work as expected.