簡體   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