Using Raku (formerly known as Perl_6)
raku -pe 's/^ (<[135]>) (.) $/$0.$1-/;'
OR
raku -pe 's/^ (.) (.) $/$0.$1-/ if m/^ <[135]> /;'
#Immediately above being a re-arrangement of:
raku -pe 'if m/^ <[135]> / {s/^ (.) (.) $/$0.$1-/};'
Posting this in the hopes that it will prove useful for Perl users who dabble in Raku. In Raku, capturing starts from $0
, and character classes are created with <[
…]>
markers (square brackets alone are reserved for grouping). Also matching is generally insensitive to whitespace (i.e. Perl5's \x
is the default).
Trying @Kusalananda's first Perl5 code example (with a Raku 'accent' as described) will produce the Raku error Null regex not allowed
. Hence the first Raku answer above looks much more like @Kusalananda's last Perl5 code example. (The second Raku answer above utilizes an if
conditional, which some users may find more readable).
Sample Input:
11
22
33
44
55
66
Sample Output:
1.1-
22
3.3-
44
5.5-
66
ADDENDUM: Since the OP posted sample data that concatenated identical digits--I am wondering if there is still an unanswered question here regarding backreferences. In Raku if you want to reuse the first capture within a match (e.g. left side of s///
operator), you do the following:
raku -pe 's/^ (<[135]>) $0 $/$0.$0-/;'
OR
raku -pe 's{^ (<[135]>) $0 $} = "$0.$0-";'
OR (named captures, below):
raku -pe 's/^ $<myOdd>=<[135]> $<myOdd> $/$<myOdd>.$<myOdd>-/;'
OR
raku -pe 's{^ $<myOdd>=<[135]> $<myOdd> $} = "$<myOdd>.$<myOdd>-";'
The second and fourth examples immediately above use Raku's new "substitute-assignment" notation, which (to enhance readability) also allows various delimiters--brackets, curlies, etc. (Of course, a separate if
conditional could be used as denoted in the earlier section, but that might result in less-readable code).
https://raku.org/archive/rfc/144.html
https://raku.org/archive/rfc/331.html
https://raku.org