繁体   English   中英

sed提取两种模式

[英]sed extract between two patterns

我需要从下面的报告中提取“定时异常”和“ ---------”之间的行,然后将它们输出到文件中

Timing exceptions with no effect exception's 
report timing -paths [eval [::legacy::get_attribute paths <exception>]]
/designs/exceptions/path_groups/
-----------------------------------

Summary
 Loop-breaking cells for combinational feedback                   0
 Nets with multiple drivers                                       0
 Timing exceptions with no effect                                 5
 Suspicious multi_cycle exceptions                                0
 Outputs without clocked external delays                          0

                                                  Total:          5

我试过sed -n '/Timing exceptions/,/-----/p' filename ,但是它总是返回

Timing exceptions with no effect exception's 
report timing -paths [eval [::legacy::get_attribute paths <exception>]]
/designs/exceptions/path_groups/
-----------------------------------
 Timing exceptions with no effect                                 5
 Suspicious multi_cycle exceptions                                0
 Outputs without clocked external delays                          0

                                                  Total:          5

我只希望上面的4行,但我不知道如何摆脱下面的行。 谁能帮我吗? 非常感谢你!

Timing exceptions之前添加^以指示该行的开始:

sed -n '/^Timing exceptions/,/-----/p' file.txt 

您在两行中都有Timing exceptions ,一个在第一行,另一个在----行之后,因此您将获得两个块,一个用于第一个,直到----行,另一个来自第二个Timing exceptions直到结束,因为之后没有----

我们在Timing exceptions之前使用了^ ,以便我们只能匹配第一个块,因为在第二个块中, Timing exceptions不在行的开头。

例:

% sed -n '/^Timing exceptions/,/-----/p' file.txt 
Timing exceptions with no effect exception's 
report timing -paths [eval [::legacy::get_attribute paths <exception>]]
/designs/exceptions/path_groups/
-----------------------------------

heemayl的答案确实完整。 但是,这是使用awk的替代方法。

awk '/^Timing exceptions/,/-----/' infile
Timing exceptions with no effect exception's
report timing -paths [eval [::legacy::get_attribute paths <exception>]]
/designs/exceptions/path_groups/
-----------------------------------

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM