Using sed, how to change the letter \'a\' to \'A\' but only if it appears repeated as two or more consecutive letters. Example, from:
galaxy
ear
aardvak
Haaaaaaa
You can do it using groups. If you have this file:
$ cat a
galaxy
ear
aardvak
Haaaaaaaaa
Ulaanbaatar
You can use this sed command:
$ sed 's/\(.\)\1\{1,\}/\U&/g' a
galaxy
ear
AArdvak
HAAAAAAAAA
UlAAnbAAtar
What does happen here? If we have a char, "packed" in a group (\(.\)
), and this group (\1
) repeats itself one or more times (\1\{1,\}
), then replace the matched part (&
) by its uppercased version (\U&
).