Search code examples
regexperlawksedword-list

Uppercase last n characters of every line in a wordlist regexp


I'm looking for a way to uppercase the last n characters of every line in a wordlist, using regular expression. Example with n=3:

Input:

thisisatest
uppercasethelast3characters

Desired output:

thisisatEST
uppercasethelast3charactERS

Solution

  • Use this GNU sed:

    sed -e 's/^\(.*\)\(.\{3\}\)$/\1\U\2/' file
    

    With extended regex:

    sed -r 's/^(.*)(.{3})$/\1\U\2/' file
    

    Test:

    $ sed -e 's/^\(.*\)\(.\{3\}\)$/\1\U\2/' file
    thisisatEST
    uppercasethelast3charactERS