简体   繁体   English

sed提取两种模式

[英]sed extract between two patterns

I need to extract the lines between "Timing exception" and "---------" from the report below, then output them to a file 我需要从下面的报告中提取“定时异常”和“ ---------”之间的行,然后将它们输出到文件中

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

I've tried sed -n '/Timing exceptions/,/-----/p' filename , but it always return 我试过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

I only want the upper 4 lines but I don't know how to get rid of the lower lines. 我只希望上面的4行,但我不知道如何摆脱下面的行。 Could anyone please help me? 谁能帮我吗? Thank you very much! 非常感谢你!

Add ^ before Timing exceptions to indicate start of the line: Timing exceptions之前添加^以指示该行的开始:

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

You have Timing exceptions in two lines, one at first, and the other after the ---- line, so you are getting two chunks, one for first one upto the ---- line, and the other from the second Timing exceptions till end as there is no line with ---- afterwards. 您在两行中都有Timing exceptions ,一个在第一行,另一个在----行之后,因此您将获得两个块,一个用于第一个,直到----行,另一个来自第二个Timing exceptions直到结束,因为之后没有----

We have used ^ before Timing exceptions so that we can only match the first chunk as in second chunk the Timing exceptions does not come at start of the line. 我们在Timing exceptions之前使用了^ ,以便我们只能匹配第一个块,因为在第二个块中, Timing exceptions不在行的开头。

Example: 例:

% 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's answer truly complete. heemayl的答案确实完整。 However, here is an alternate approach using awk . 但是,这是使用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