How to replace one letter of the first letter n with uppercase

I would like to replace the first n-letter with upper case with one sed command.

Example "madrid" in "MADrid". (N = 3)

I know how to change the first letter to uppercase with this command:

    sed -e "s/\b\(.\)/\U\1/g"

but I don’t know how to change this command for my problem.

I tried to change

    sed -e "s/\b\(.\)/\U\1/g"

to

    sed -e "s/\b\(.\)/\U\3/g"

but it didn’t work. Also, I googled and searched on this site, but an exact answer with my problem that I could not find.

Thank.

+4
source share
2 answers

I \Uget out of your use that you use GNU sed:

n=3
echo 'madrid' | sed -r 's/\<(.{'"$n"'})/\U\1/g'  # -> 'MADrid'

3 .

$n :

sed -r 's/\<([[:alpha:]]{1,'"$n"'})/\U\1/g'

:

\3 sed -e "s/\b\(.\)/\U\3/g" ( , (...)) ( ), .

, ( \1 ) , - , {<n>}; {<m>,<n>} .

+4

(GNU sed):

 sed -r 's/[a-z]/&\n/'"$n"';s/^([^\n]*)\n/\U\1/' file

$n - n . , n a-z , .. a-z

N.B. sed !

0

Source: https://habr.com/ru/post/1663877/


All Articles