如何使用 grep 搜索文件中的段落,然后从该段落中搜索特定的单词
How to Search a paragraph in a file using grep and then search a specific word from the paragraph
我想从文件中搜索以下段落,然后显示该段落中的特定单词。
例如
"Documentation can be done using
MS excel and an eg. of such a file is sample.xls"
我不想从文件中搜索上面的段落 example.log 然后只想显示单词 "sample.xls"
可以吗???
如果你的 grep 支持 -P
, perl-regexp 参数那么你可以简单地使用下面的 grep 命令。
$ grep -ozP '(?:.+\n)*.*\bDocumentation can\b.*(?:\n.+)*' file | grep -o '[^ ]\+\.xls\b'
sample.xls
第一个 grep 用于获取特定段落,下一个 grep 用于从该段落获取具有特定扩展名的文件。
我想从文件中搜索以下段落,然后显示该段落中的特定单词。 例如
"Documentation can be done using
MS excel and an eg. of such a file is sample.xls"
我不想从文件中搜索上面的段落 example.log 然后只想显示单词 "sample.xls"
可以吗???
如果你的 grep 支持 -P
, perl-regexp 参数那么你可以简单地使用下面的 grep 命令。
$ grep -ozP '(?:.+\n)*.*\bDocumentation can\b.*(?:\n.+)*' file | grep -o '[^ ]\+\.xls\b'
sample.xls
第一个 grep 用于获取特定段落,下一个 grep 用于从该段落获取具有特定扩展名的文件。