首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何根据模式输出特定段落?

如何根据模式输出特定段落?
EN

Unix & Linux用户
提问于 2016-09-23 07:35:41
回答 2查看 1.5K关注 0票数 0

下面有一个示例文件:

代码语言:javascript
运行
复制
# 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和输出?如果文件有更多的段落,我想输出包含单词“太”的段落。

EN

回答 2

Unix & Linux用户

发布于 2016-09-23 08:21:27

如果段落为空行分隔:

代码语言:javascript
运行
复制
awk -v RS= -v ORS='\n\n' /TOO/

空记录分隔符(RS)是指用空行序列分隔记录的段落模式。

如果它们是#分离的:

代码语言:javascript
运行
复制
awk -v RS='#' '/TOO/ {print RS $0}'

代码语言:javascript
运行
复制
pcregrep -Mo '#[^#]*?TOO[^#]*'
  • 用于多行-Mgrep
  • -o只输出匹配的部分
票数 2
EN

Unix & Linux用户

发布于 2016-09-23 08:29:03

代码语言:javascript
运行
复制
perl -00ne 'print if /TOO/'
  • -00表示段落模式(记录由一个或多个空行分隔)。
票数 0
EN
页面原文内容由Unix & Linux提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://unix.stackexchange.com/questions/311786

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档