Using Raku (formerly known as Perl_6)
Alphabetize letter options in ASCII-order (uppercase first):
~$ raku -pe 's{ <?after \, > (\w+) } = "{$0.comb.sort.join}";' file
Raku is a programming language in the Perl-family that features high-level support for Unicode built-in. Above is a Raku translation of excellent Perl code by @steeldriver (with a nod to @StéphaneChazelas).
- The
s/// form can be written s{ … } = " … ". Curlies in the replacement half (inside the doublequotes) signify a code block that runs on the capture. - Captures in Raku start from
$0. - A positive lookbehind "Y-after-X" in Raku is written
<?after X > Y (the negative uses a ! instead of a ?). - Raku implements a positive (global) selector function
comb, that--absent a regex argument--breaks on every character.
Sample Input:
rap ,Xflg MIT X11 rap ,XPBfl 'MITER' rap ,Bflg share git-grep rap ,bfl X11 rap ,Bfl xzfgrep rap ,Bf X11
Sample Output (1):
rap ,Xfgl MIT X11 rap ,BPXfl 'MITER' rap ,Bfgl share git-grep rap ,bfl X11 rap ,Bfl xzfgrep rap ,Bf X11
Alphabetize letter options with lowercase first:
~$ raku -pe 's{ <?after \, > (\w+) } = "{ my (@l,@u); $0.comb.map({ /<:Ll>/ ?? @l.push($_) !! @u.push($_) }); join "", @l.sort,@u.sort }";' file
OR:
~$ raku -pe 's{ <?after \, > (\w+) } = "{ $0.comb.classify: { /<:Ll>/ ?? "lc" !! "uc" }, :into( my %case{Str} ); $_.join given %case.sort.map: *.value.sort.join }";' file
Sample Output (2):
rap ,fglX MIT X11 rap ,flBPX 'MITER' rap ,fglB share git-grep rap ,bfl X11 rap ,flB xzfgrep rap ,fB X11
Above uses the Unicode representation of lowercase letter, <:Ll> (see first link below for details). Also on display is Raku's ternary operator: Test ?? True !! False (see second link below for details). The two answers differ in that the first uses two arrays (directly) with a ternary, while the second uses a single hash (created via classify).
NOTE: Thanks to a comment by @StéphaneChazelas, it's probably better to use (<alpha>+) as the capture, instead of (\w+).
https://docs.raku.org/language/regexes#Predefined_character_classes
https://docs.raku.org/language/operators#infix_??_!!
https://docs.raku.org/routine/classify
https://docs.raku.org/language/regexes
https://raku.org
getoptsto parse the options? That would give you the expected behaviour (with regards to parsing command-line options) of yourrapfunction and minimize the amount of extraneous code that you would need to maintain.rap ,B ,f X11and, if so, should it be considered equivalent torap ,Bf X11?