显示文本文件中匹配表达式的第二段

时间:2015-05-01 18:10:00

标签: linux awk grep expression aix

我需要grep和awk的帮助。尝试显示与搜索表达相关的一些文本,但要显示的文本位于单独的段落中。

这是我文本文件中的文字格式:

Application :
  Address :                0x07000039435882C0
  AppHandl [nod-index] :   52473    [000-52473]
  TranHdl :                132
  Application PID :        0
  Application Node Name :
IP Address: 8 Connection Start Time : (1429703458)Wed Apr 22 07:50:58 2015 Client User ID : n/a System Auth ID : MMSCORE Coordinator EDU ID : 315757 Coordinator Partition : 0 Number of Agents : 1 Locks timeout value : NotSet Locks Escalation : No Workload ID : 1 Workload Occurrence ID : 266654 Trusted Context : n/a Connection Trust Type : non trusted Role Inherited : n/a Application Status : UOW-Executing Application Name : db2jcc_application Application ID :
TranHdl : 132 Application PID : 0 Application Node Name :
IP Address:
Connection Start Time : (1429703458)Wed Apr 22 07:50:58 2015 Client User ID : n/a System Auth ID : MMSCORE Coordinator EDU ID : 315757 Coordinator Partition : 0 Number of Agents : 1 Locks timeout value : NotSet Locks Escalation : No Workload ID : 1 Workload Occurrence ID : 266654 Trusted Context : n/a Connection Trust Type : non trusted Role Inherited : n/a Application Status : UOW-Executing Application Name : db2jcc_application Application ID : 169.83.188.178.49385.150423170149 ClientUserID : n/a ClientWrkstnName :
ClientApplName : n/a ClientAccntng : n/a CollectActData: N CollectActPartition: C SectionActuals: N

List of active statements : *UOW-ID : 6345 Activity ID : 2 Package Schema : MMSCORE Package Name : P0465265 Package Version : Section Number : 1 SQL Type : Static Isolation : CS Statement Type : DML, Select (blockable)

*UOW-ID : 689 Activity ID : 39 Package Schema : PROF Package Name : P366175099 Package Version : Section Number : 35 SQL Type : Static Isolation : CS Statement Type : Top-level SET, no SQL

UOW-ID :          689
Activity ID :     1
Package Schema :  NULLID
Package Name :    SYSSN300
Package Version :
Section Number :  1
SQL Type :        Dynamic
Isolation :       RS
Statement Type :  CALL
Statement :       call
PKG_FULL_PROFILE_V3.SP_LIST_PROFILE (?, ?, ?, ?)

i will be searchibg for AppHandl value which is 52473, once found, i would like to display every paragraph starting with UOW-ID.

In the text file, i will have many entries starting with Application: paragraph as above, so i want only to display all UOW_ID for that specific application handle i am searching.

so the output will look like this based on the text file i pasted above when i search for specific AppHandle:

List of active statements : *UOW-ID : 6345 Activity ID : 2 Package Schema : MMSCORE Package Name : P0465265 Package Version : Section Number : 1 SQL Type : Static Isolation : CS Statement Type : DML, Select (blockable)

*UOW-ID : 689 Activity ID : 39 Package Schema : PROF Package Name : P366175099 Package Version : Section Number : 35 SQL Type : Static Isolation : CS Statement Type : Top-level SET, no SQL

UOW-ID : 689 Activity ID : 1 Package Schema : NULLID Package Name : SYSSN300 Package Version : Section Number : 1 SQL Type : Dynamic Isolation : RS Statement Type : CALL Statement : call PKG_FULL_PROFILE_V3.SP_LIST_PROFILE (?, ?, ?, ?)

感谢您的帮助!!!

1 个答案:

答案 0 :(得分:1)

试试这个AWK脚本:

{
    if($0 ~ /AppHandl/) {
        if($0 ~ /AppHandl.*52473/) {
            found = 1
        }
        else {
            found = 0
        }
    }

    if(found) {
        if($0 ~ /UOW-ID/) {
            paragraph = 1
            print prev
        }
        if($0 ~ /^$/) {
            paragraph = 0
            printf "\n"
        }
    }
    if(found && paragraph) {
        print
    }
    prev = $0
}

找到AppHandl值52473后,它会打印以OUW-ID开头的所有段落,直到找到具有不同值的AppHandle。这就是我理解你的要求的方式。

它产生的输出与编辑问题中的请求相同。如果您需要进行一些外观修改,例如不同的格式,我希望您能够自己修改我的示例。