简体   繁体   中英

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.

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

This is an approach via sed . Its not a short command but better to understand I think.

sed -e 's/\![[:space:]]/\!\n/g' \
-e 's/\?[[:space:]]/\?\n/g' \
-e 's/\.[[:space:]]/\.\n/g' | \
grep -v '^[[:lower:]]'
This is the first sentence.
This is the second sentence!
Is this the third sentence?

Explanation:

First thee set commands looking for that punctuation mark followed by white space \:[[:space:]] and replace them with that same punctuation mark and a new line \!\n . At last grep is looking through all lines and remove the ones starting with a lowercase letter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM