sed -e '/^AUTH:\([^[:alpha:]]*\)/!b' -e 'h;s//\1/;x;s///
s/\([[:alpha:]]\)\([[:alpha:]]*[^[:alpha:]]*\)/\1/g;x;s//\
\2/g
y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/;G;:l
s/\n\(.*\n\)\(.\)/\2\1/;tl
s/\(.*\)\n/AUTH:\1/
'<<\IN
TITLE: Average title
AUTH: SUPERMAN
AFF: Something
AUTH: THE NEW ONE
AFF: Berlin
AUTH: MARS-MENSCH
AFF: Planet Mars
AUTH: CONTRARY tO pOPULAR bELIEF, Lorem Ipsu'M is not simply random text. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance.
IN
Portably to translate characters with sed
you need to use the y///
translate command and to explicitly set out each character to be translated. This actually does come in handy - there's very little confusion and/or guessing in that way.
This script splits each AUTH: line into two bits - One gets the first alphabetic character in any series of one-or-more alphabetic characters replaced with a \n
ewline. The other bit consists of nothing but those first alphabetics and is h
eld while the line is translated. After translation sed
G
ets the held bit, and replaces each \n
ewline character with the first character following the last \n
ewline in a loop until none remain.
Here's a l
ook at what the Lorem Ipsum line above looks like just before sed
begins the replace loop:
\nontrary \no \nopular \nelief, \norem \npsum \ns \not \nimply \nand\
om \next. \norem \npsum \nomes \nrom \nections 1.10.32 \nnd 1.10.33 \
\nf "\ne \ninibus \nonorum \nt \nalorum" (\nhe \nxtremes \nf \nood \n\
nd \nvil) \ny \nicero, \nritten \nn 45 \nc. \nhis \nook \ns \n \nreat\
ise \nn \nhe \nheory \nf \nthics, \nery \nopular \nuring \nhe \nenais\
sance.\nCtpbLIinsrtLIcfsaodFBeMTEoGaEbCwiBTbiatottoevpdtR$
The AUTH: bit is stripped when the line is confirmed - it is reinserted last - and so it is not there, but the space that follows it - and whatever other chars that may come between - is there. You can see that all of the words begin with \n
ewlines - sed
trades those in order for each char in the string following the last one. sed
doesn't distinguish between upper and lower case when saving the first letter of each word - they are all put aside and replaced later.
OUTPUT:
TITLE: Average title
AUTH: Superman
AFF: Something
AUTH: The New One
AFF: Berlin
AUTH: Mars-Mensch
AFF: Planet Mars
AUTH: Contrary to popular belief, Lorem Ipsu'M is not simply random text. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 Bc. This book is a treatise on the theory of ethics, very popular during the Renaissance.