SED:印刷两个词之间的界线,只有当该行的一个相匹配的第三个词或任何图案 [英] sed : printing lines between two words only when one of the line matches a third word or any pattern

查看:109
本文介绍了SED:印刷两个词之间的界线,只有当该行的一个相匹配的第三个词或任何图案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道的话Foo和Bar之间的距离的test.txt使用以下命令SED打印行

  SED -n'/ FOO /,/酒吧/ P'的test.txt

但我怎么让SED打印Foo和Bar之间的界限,只有当其中一条线有匹配模式

例如,具有的text.txt以下行的文件:

 错误 - 未定义的端口
一号线
2号线
在ALU1未定义端口
3号线错误 - 未定义的端口
4号线
LINE5
在LSU未定义端口
LINE6错误 - 未定义的端口
line7
line8
在FGU未定义端口
line9错误 - 未定义的端口
line10
line11
在ALU2未定义端口
line12

我想打印出的两个连续出现的线
 单词错误只有当一条线路包含单词ALU。

所以我只是想打印出以下错误消息:

 错误 - 未定义的端口
一号线
2号线
在ALU1未定义端口
3号线错误 - 未定义的端口
line10
line11
在ALU2未定义端口
line12


解决方案

要实现这一点,需要分支的sed脚本并保持缓冲。

该脚本使用两个缓冲器:图案缓冲液(它是在那里的sed存储当前处理的行,这是用于模式匹配的测试缓冲液)和保持缓冲器(缓冲intented用于存储previous线)。思想是存储从最后一个 /错误/ 模式匹配的所有行,并检查了 / ALU / occurence接下来的时间/错误/ 匹配或流的末尾。

  SED -n
#如果/错误/图案发生,跳到/ ALU /检查
/错误/ B alu_check
#否则当前行追加到保持缓冲器
H
#如果当前行是最后一个,跳到/ ALU /检查
$ B alu_check
#否则跳转到脚本的结束(=该行的精加工)
b
#alu_check:
:alu_check
与hols缓冲环境#交换电流模式缓冲区
X
#打印previous记录,如果/ ALU /发生
/ ALU / p

X 为了交流,保持缓冲上下文模式缓冲区上下文(当前行)(什么是从最后一次记忆) - 请注意,它存储当前行与/错误/模式来保持缓冲区中的下一次

^ h 追加当前行上下文保持缓冲

i know sed prints lines between words FOO and BAR from test.txt using the following command

  sed -n '/FOO/,/BAR/p' test.txt

but how do i make sed print the lines between FOO and BAR only when one of the lines has a matching pattern

For example, the file text.txt has the following lines :

Error- Undefined port
line1
line2
Undefined port in ALU1 
line3

Error- Undefined port
line4
line5
Undefined port in LSU 
line6

Error- Undefined port
line7
line8
Undefined port in FGU 
line9 

Error- Undefined port
line10
line11
Undefined port in ALU2 
line12

i want to print out lines between the two successive occurrences of the word "Error" only when one of the lines contain the word "ALU".

so i just want to print out the following Error messages :

Error- Undefined port
line1
line2
Undefined port in ALU1 
line3

Error- Undefined port
line10
line11
Undefined port in ALU2 
line12

解决方案

To achieve this, you need branching in the sed script and hold buffer.

The script uses two buffers: pattern buffer (it is the buffer where sed stores currently processed line and which is used for the pattern matching tests) and the hold buffer (buffer intented for storing previous lines). The thought is to store all lines from the last /Error/ pattern match and check the /ALU/ occurence in the time of next /Error/ match or the end of stream.

sed -n '
# if /Error/ pattern occured, jump to /ALU/ check
/Error/ b alu_check
# else append current line to the hold buffer
H
# if the current line is the last one, jump to /ALU/ check
$ b alu_check
# otherwise jump to end of script (= finish processing of this line)
b
# alu_check:
:alu_check
# exchange current pattern buffer with hols buffer context
x
# print previous record if /ALU/ occured
/ALU/ p
'

x order exchanges the pattern buffer context (current line) with hold buffer context (what is remembered from the last time) - notice that it stores current line with /Error/ pattern to the hold buffer for the next time

H appends current line context to the hold buffer

这篇关于SED:印刷两个词之间的界线,只有当该行的一个相匹配的第三个词或任何图案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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