0

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.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Tommy
  • 159
  • 1
  • 11

3 Answers3

3

Try this:

sed -r "s/(^|\.\s+)./\U&/g" <input.txt >output.txt
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • Hello, I have found that it don't work with all UTF8 symbols. This is for Slovak system and sentences sometimes starting with ľ š č ť ž ý á í é ď etc. I ideas to solve this issue? – Tommy May 26 '13 at 08:53
  • It should work if your locale is set correctly (see [here](http://stackoverflow.com/a/67575/1630171)). – Ansgar Wiechers May 26 '13 at 08:59
  • Well i changed my locale to slovak utf8 and it dont work.. it replacing special characters with some robish characters. Any ideas? – Tommy May 26 '13 at 15:24
  • I checked with german UTF-8 locale and it worked just fine. Did you adjust `LANG` and `LC_*`? Does your terminal support UTF-8? – Ansgar Wiechers May 26 '13 at 23:17
1

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
jaypal singh
  • 74,723
  • 23
  • 102
  • 147
  • Thank you it works, but it won't work with sentences starting with ľ š č ť ž ý á í é ď etc. Any ideas? – Tommy May 26 '13 at 15:27
  • @Tommy `bash v4` handles it. If it doesn't then your `locale` isn't set to handle this. You probably need to at least set `LC_CTYPE` to a `UTF-8` variation. – jaypal singh May 26 '13 at 16:11
0

What about awk way?

$ awk -F"\. " '{OFS=". "}{for (i=0;i<=NF;i++) {sub(".", substr(toupper($i), 1,1) , $i)}} {print}' output.txt 
This is my first sentence. And this is the second sentence. That one is the third.
  • -F"\. " sets the field delimiter to . (dot + space).
  • {OFS=". "} sets the output field delimiter to . (dot + space).
  • '{for (i=0;i<=NF;i++) {sub(".", substr(toupper($i), 1,1) , $i)}} loops through each field capitalizing the first word of them. As first field is this is my first sentence, it just capitalizes this.
fedorqui
  • 275,237
  • 103
  • 548
  • 598