I am trying to capitalize each first letter of the first word in each sentence from a txt file called input.txt and I want this input file to be a argument of the shell script
./script.sh input.txt
sample input file:
i am Andrew. you are Jhon. here we are, forever.
result file:
I am Andrew. You are Jhon. Here we are, forever.
A special case. What if our text is (related to @RaduRadeanu answer)
i am andrew. you
are jhon. here we are
forever
the result would be:
I am andrew. You
Are jhon. Here we are
Forever.
So it convert to uppercase each first word of each sentence and also each first word of new line. How do we skip over uppercase first word of new line?
So the correct result must be:
I am andrew. You
are jhon. Here we are
forever.
What if the sentence closes in "?" or "!" ???
sed
command is very powerful to edit files from shell scripts. With its help you can edit however you want a text file. These being said, the following script can do what you wish:For your special case, things became slightly:
Also, you can consult this tutorial: Unix - Regular Expressions with SED to see how to work in these situations.
How about using bash's builtin 'read' function with the period character as delimiter to read each whole sentence into a variable, and then capitalizing the initial character of the variable? Something like
To handle multiple sentence terminators e.g. ? and ! as well as the regular period, here is a different approach using 'awk' - note that the RT variable that allows us to recover the particular record terminator which matched a particular sentence is an extension that may not be available in all varieties of 'awk'
Note that the record separator regex above will handle multiple consecutive delimiters ('!?!!!') and optional trailing spaces - which the read-based version doesn't.
As a further enhancement, let's try to add rudimentary handling of quoted sentences by modifying the RS regex once more and changing the sub so that it upper-cases the first non-quote character:
e.g.