Search code examples
regexbashdirectorygrep

Bash script to find a sentence pattern


I'd like to have a script that when there is a text with multiple sentences as a stdin, it would write each sentence on a new line to a stdout. That means that it would only print out those parts that begin with a capital letter and end with only one of the punctuation marks: dot/exclamation/question mark.

Example:

Stdin:

This is the first sentence. This is the second sentence! Is this the third sentence? this is not a sentence

Stdout:

This is the first sentence.
This is the second sentence!
Is this the third sentence?
while read -r INPUT
do
    if [[ "$SENFLAG" == "1" ]]
    then
        echo "$INPUT" | grep -o '[[:alpha:]][^ ]*[A-Z][^ ]*' 
    fi
done

I tried working with grep, but I am not sure how to advance further.


Solution

  • grep -Eo '[A-Z][^.!?]*[.!?]' input_file