小编Ama*_*eur的帖子

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

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

text="This is sentence! this is not sentence! This is sentence. this is not sencence."
Run Code Online (Sandbox Code Playgroud)

我正在通过这个命令寻找句子:

echo $text | awk 'match($0,/([A-Z])([^!?.]*)([!?.])/) { print substr($0,RSTART,RLENGTH) }'
Run Code Online (Sandbox Code Playgroud)

我的输出是:

This is sentence!
Run Code Online (Sandbox Code Playgroud)

预期输出:

This is sentence!
This is sentence.
Run Code Online (Sandbox Code Playgroud)

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

text="incorrect sentence! this is not sentence! This is sentence. this is not sencence. This is correct sentence."
Run Code Online (Sandbox Code Playgroud)

预期输出:

This is sentence.
This is correct sentence.
Run Code Online (Sandbox Code Playgroud)

我能够找到第一个匹配项,但不是全部。感谢您的帮助 :)

awk echo

1
推荐指数
1
解决办法
87
查看次数

标签 统计

awk ×1

echo ×1