Search code examples
bashcapitalizationcapitalize

Replace first symbol of sentence with capital letter


I need help with capitalizing the first letter of the first word in sentences from an input file input.txt:

this is my first sentence. and this is the second sentence. that one is the third.

I want to make the output look like this in an output file output.txt:

This is my first sentence. And this is the second sentence. That one is the third.


Solution

  • bash version 4 way:

    #!/usr/local/bin/bash
    
    while IFS="." read -r -a line ; do
        for ((i=0; i<${#line[@]}; i++)) do
            if [[ $i > 0 ]]; then
                temp=$(echo ${line[$i]/ /})
                echo -n "${temp^}. "
            else
                echo -n "${line[$i]^}. "
            fi
        done
        echo
    done < file