如何在文件中查找模式并在找到模式时打印某些内容?

如何在文件中查找模式并在找到模式时打印某些内容?

例子 -

测试日志:

ConductID: null; RunOpSequenceCode: 12345678; ClientID: null; FescoOfferID: null; PriorityCode: null; RowNumberStart: 0; FetchSize: 0; ChannelTypes: ER,PART; NigoIndicator: null; TreatmentTypeCode: null; DivisionName: null; DivisionCode: null; }
[11-21-2099 23:25:28] [OK #6:DAMN_BATCH ] T123Util             setT123Columns 
ConductID: null; RunOpSequenceCode: 12345678; ClientID: null; FescoOfferID: null; PriorityCode: null; RowNumberStart: 0; FetchSize: 0; ChannelTypes: ER,PART; NigoIndicator: null; TreatmentTypeCode: null; DivisionName: null; DivisionCode: null; }
[11-21-2099 23:25:28] [OK #6:DAMN_BATCH ] T123Util             setT123Columns 
ConductID: null; RunOpSequenceCode: 12345678; ClientID: null; FescoOfferID: null; PriorityCode: null; RowNumberStart: 0; FetchSize: 0; ChannelTypes: ER,PART; NigoIndicator: null; TreatmentTypeCode: null; DivisionName: null; DivisionCode: null; }
[11-21-2099 23:25:28] [OK #6:DAMN_BATCH ] T123Util             setT123Columns       WARN : UserDefined Tatty Error: SqlMapClient operation; uncategorized SQLException for SQL []; SQL state [72000]; error code [20001];
--- The error occurred in com//tmo/asfjn/jdaf/tkenjrt/terkj.xml
--- The error occurred while eating sandwich.
--- Check the tim-tim-nothing-boy.
--- Cause: java.sql.SQLException: ORA-00001: You suck.
ORA-06512: at "test_amp_tim.pkg", line 78

现在我希望脚本查找“UserDefined Tatty Error”并从“发生错误......”一直打印到“你太糟糕了”。

我尝试了 grep -Fxq 和 sed,但没有成功。谢谢。

答案1

像这样的东西吗?

awk '# If we see the error, set t to 1
    /UserDefined Tatty Error/ { t=1; next }
    # If it's not a --- reset t to 0 (even if it's already 0)
    !/^--- / { t=0 }
    # If t==1 we are inside the error message; so, print
    t' file

答案2

awk '/UserDefined Tatty Error/,/You suck/' file

相关内容