[英]Find if a string starts with a specific pattern using regex in bash script
[英]Bash script to find a sentence pattern
我想要一個腳本,當有一個包含多個句子的文本作為標准輸入時,它會在一個新行上將每個句子寫入一個標准輸出。 這意味着它只會打印出那些以大寫字母開頭且僅以標點符號之一結尾的部分:點/感嘆號/問號。
例子:
標准輸入:
This is the first sentence. This is the second sentence! Is this the third sentence? this is not a sentence
標准輸出:
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
我嘗試使用 grep,但我不確定如何進一步推進。
grep -Eo '[A-Z][^.!?]*[.!?]' input_file
這是通過sed
的一種方法。 這不是一個簡短的命令,但我認為更好理解。
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?
解釋:
首先, set
命令查找標點符號后跟空格\:[[:space:]]
並將它們替換為相同的標點符號和新行\!\n
。 最后grep
正在查看所有行並刪除以小寫字母開頭的行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.