Using Raku (formerly known as Perl_6)
~$ echo 'a\nb'
a\nb
~$ echo 'a\nb' | raku -pe 's:g/ <!after "\\"> (.) /{$0.uc}/;'
A\nB
~$ echo "a\\nb"
a\nb
~$ echo "a\\nb" | raku -pe 's:g/ <!after "\\"> (.) /{$0.uc}/;'
A\nB
Above uses a negative look-behind assertion, <!after "\\">
, to select out all characters except those immediately after a \
backslash. Selected characters are then uppercased with Raku's .uc
routine.
Certainly it's safer to provide the regex with a custom <-[ … ]>
negative character class, sparing backslashed characters like \n
and \t
from being uppercased. (FYI, custom positive character classes are written <+[ … ]>
or more simply <[ … ]>
in Raku).
Below, using Raku's "Q-lang" (quoting language) to feed the substitution operator a string. In all four examples below \n
is returned (not uppercase \N
). Note in the third example how \n
is operationally-interpreted as a newline character, and this remains unchanged in the fourth example, telling us that \n
still exists in that string (i.e. it has NOT been uppercased to \N
):
~$ raku -e 'put Q<a\nb>'
a\nb
~$ raku -e 'put Q<a\nb>' | raku -pe 's:g/ <!after "\\"> (<-[nt]>) /{$0.uc}/;'
A\nB
~$ raku -e 'put Q:b<a\nb>'
a
b
~$ raku -e 'put Q:b<a\nb>' | raku -pe 's:g/ <!after "\\"> (<-[nt]>) /{$0.uc}/;'
A
B
NOTE, see: "Place an escape sign before every non-alphanumeric characters" for Raku answers to a related question on StackOverflow.
References:
https://docs.raku.org/language/quoting
https://docs.raku.org/language/regexes#Literals_and_metacharacters
https://raku.org
ascii_upcase
in tho JSON parserjq
, or the XPath functionupper-case()
for XML. – Kusalananda Jul 24 '22 at 11:50