下面有一个示例文件:
# This is a test file. This is a test file. This is a test file
This is a test file. This is a test file. This is a test file.
This is a test file.
# Need to output just this paragraph.Need to output just this paragraph.
Need to output just this paragraph TOO. Need to output just this paragraph.
Need to output just this paragraph.
我只需要输出从"#“到该段最后一句的第二段。
如何基于模式实现grep和输出?如果文件有更多的段落,我想输出包含单词“太”的段落。
发布于 2016-09-23 00:21:27
如果段落为空行分隔:
awk -v RS= -v ORS='\n\n' /TOO/
空记录分隔符(RS
)是指用空行序列分隔记录的段落模式。
如果它们是#
分离的:
awk -v RS='#' '/TOO/ {print RS $0}'
或
pcregrep -Mo '#[^#]*?TOO[^#]*'
-M
的grep
-o
只输出匹配的部分发布于 2016-09-23 00:29:03
perl -00ne 'print if /TOO/'
-00
表示段落模式(记录由一个或多个空行分隔)。https://unix.stackexchange.com/questions/311786
复制相似问题