如何在bash中查找和打印所有AWK匹配?

如何在bash中查找和打印所有AWK匹配?,awk,echo,Awk,Echo,我在变量中存储了很多文本 text="This is sentence! this is not sentence! This is sentence. this is not sencence." 我正在通过以下命令查找句子: echo $text | awk 'match($0,/([A-Z])([^!?.]*)([!?.])/) { print substr($0,RSTART,RLENGTH) }' 我的结论是: This is sentence! 预期产出:

我在变量中存储了很多文本

text="This is sentence! this is not sentence! This is sentence. this is not sencence."
我正在通过以下命令查找句子:

echo $text | awk 'match($0,/([A-Z])([^!?.]*)([!?.])/) { print substr($0,RSTART,RLENGTH) }'
我的结论是:

This is sentence!
预期产出:

This is sentence!
This is sentence.
This is sentence.
This is correct sentence.
更多示例: 课文中有语法正确的句子和语法错误的句子。正确的句子由开头的大写字母和结尾字符(.?!)标识。我只想打印正确的句子

text="incorrect sentence! this is not sentence! This is sentence. this is not sencence. This is correct sentence."
预期产出:

This is sentence!
This is sentence.
This is sentence.
This is correct sentence.
我能找到第一场比赛,但不是全部。谢谢您的帮助:)

您需要一个带有
match()的
while()

输出:

This is sentence!
This is sentence.
您需要一个带有
match()
while()

输出:

This is sentence!
This is sentence.

您可以将GNU awk用于多字符:

$ echo "$text" | awk -v RS='[A-Z][^!?.]*[!?.]' 'RT{print RT}'
This is sentence!
This is sentence.
或用于FPAT的GNU awk:

$ echo "$text" | awk -v FPAT='[A-Z][^!?.]*[!?.]' '{for (i=1; i<=NF; i++) print $i}'
This is sentence!
This is sentence.

如果一个句子可以包含换行符,则只有上述第一项有效。

您可以将GNU awk用于多字符:

$ echo "$text" | awk -v RS='[A-Z][^!?.]*[!?.]' 'RT{print RT}'
This is sentence!
This is sentence.
或用于FPAT的GNU awk:

$ echo "$text" | awk -v FPAT='[A-Z][^!?.]*[!?.]' '{for (i=1; i<=NF; i++) print $i}'
This is sentence!
This is sentence.
如果一个句子可以包含换行符,则只有上面的第一条才有效