我怎样才能用awk或者sed找到两个词之间的匹配模式 [英] how can I find a matching pattern between two words using sed or awk

查看:218
本文介绍了我怎样才能用awk或者sed找到两个词之间的匹配模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要搜索段落与WORD1开始的模式并用字词2结束,如果打印的模式匹配段落的首行,我不知道如果我可以用grep做
例如,如果我有以下的文件,我正在寻找AAA

 字1这是款号1
AAA
BBB
CCC
WORD2字1这是第2号
BBB
CCC
DDD
WORD2

答案应该是这样的。

 字1这是款号1


解决方案

您可以试试这个 AWK

 的awk'/ ^字1 / {F = 1; L =;举办= $ 0} / $字词2 / {F = 0;如果(l〜/ AAA /){打印保持}} F到{1 = L RS $ 0}'文件

I want to search a pattern in paragraph that begins with word1 and end with word2 and print the first line of the paragraph if the pattern match, I am not sure if I can do it using grep for example if I have the following file and I am looking for aaa

Word1 this is paragraph number 1 
aaa
bbb
ccc
word2

Word1 this is paragraph number 2 
bbb
ccc
ddd
word2

the answer should be like that

Word1 this is paragraph number 1

解决方案

You can try this awk:

awk '/^Word1/{f=1;l="";hold=$0} /word2$/{f=0; if(l ~ /aaa/){print hold}} f{l = l RS $0}' file

这篇关于我怎样才能用awk或者sed找到两个词之间的匹配模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆