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 a 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.
CodePudding user response:
Using extended grep
$ grep -Eo '[A-Z][^.!?]*[.!?]' input_file
This is the first sentence.
This is the second sentence!
Is this the third sentence?
Match only strings that begin with a capital letter and end with a .!?
CodePudding user response:
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.