打印数组或文件的一部分并在shell中排序

时间:2016-08-16 17:11:06

标签: bash shell awk

我有一个文本文件如下

     1 : dis conn(*) type(CONN) all where(UOWLOG ne '')
AMQ8276: Display Connection details.
   CONN(AE1CA9572C47D822)                
   EXTCONN(414D51434D5147575032202020202020)
   TYPE(CONN)                            
   PID(9098)                               TID(503215) 
   APPLDESC(WebSphere MQ Channel)       
   APPLTAG(WebSphere MQ Client for Java)
   APPLTYPE(USER)                          ASTATE(NONE)
   CHANNEL(ABIS.CLIENT)           CLIENTID( )
   CONNAME(101.54.151.80)               
   CONNOPTS(MQCNO_HANDLE_SHARE_BLOCK,MQCNO_FASTPATH_BINDING)
   USERID(malth)                         UOWLOG(S0599315.LOG)
   UOWSTDA(2016-08-16)                     UOWSTTI(11.06.38)
   UOWLOGDA(2016-08-16)                    UOWLOGTI(11.06.38)
   URTYPE(QMGR)                         
   EXTURID(XA_FORMATID[] XA_GTRID[] XA_BQUAL[])
   QMURID(0.190537069)                     UOWSTATE(ACTIVE)
AMQ8276: Display Connection details.
   CONN(AE1CA957D787F429)                
   EXTCONN(414D51434D5147575032202020202020)
   TYPE(CONN)                            
   PID(28292)                              TID(1519092) 
   APPLDESC(WebSphere MQ Channel)       
   APPLTAG(WebSphere MQ Client for Java)
   APPLTYPE(USER)                          ASTATE(NONE)
   CHANNEL(SB.CLIENT)                  CLIENTID( )
   CONNAME(101.54.151.119)              
   CONNOPTS(MQCNO_HANDLE_SHARE_BLOCK,MQCNO_FASTPATH_BINDING)
   USERID(sbp)                         UOWLOG(S0599317.LOG)
   UOWSTDA(2016-08-16)                     UOWSTTI(11.07.38)
   UOWLOGDA(2016-08-16)                    UOWLOGTI(11.07.38)
   URTYPE(QMGR)                         
   EXTURID(XA_FORMATID[] XA_GTRID[] XA_BQUAL[])
   QMURID(0.190520281)                     UOWSTATE(ACTIVE)
AMQ8276: Display Connection details.
   CONN(2CC7AE57CE8E6221)                
   EXTCONN(414D51434D5147575032202020202020)
   TYPE(CONN)                            
   PID(22400)                              TID(647335) 
   APPLDESC(WebSphere MQ Channel)       
   APPLTAG(WebSphere MQ Client for Java)
   APPLTYPE(USER)                          ASTATE(NONE)
   CHANNEL(U90785.CBM.CLIENT)              CLIENTID( )
   CONNAME(101.54.163.37)               
   CONNOPTS(MQCNO_HANDLE_SHARE_BLOCK,MQCNO_FASTPATH_BINDING)
   USERID(machlth)                         UOWLOG(S0599317.LOG)
   UOWSTDA(2016-08-16)                     UOWSTTI(11.07.40)
   UOWLOGDA(2016-08-16)                    UOWLOGTI(11.07.40)
   URTYPE(QMGR)                         
   EXTURID(XA_FORMATID[] XA_GTRID[] XA_BQUAL[])
   QMURID(0.190574800)                     UOWSTATE(ACTIVE)
AMQ8276: Display Connection details.
   CONN(2CC7AE571BF0B721)                
   EXTCONN(414D51434D5147575032202020202020)
   TYPE(CONN)                            
   PID(22210)                              TID(1069968) 
   APPLDESC(WebSphere MQ Channel)       
   APPLTAG(WebSphere MQ Client for Java)
   APPLTYPE(USER)                          ASTATE(NONE)
   CHANNEL(APP.CLIENT)                  CLIENTID( )
   CONNAME(101.54.163.127)              
   CONNOPTS(MQCNO_HANDLE_SHARE_BLOCK,MQCNO_FASTPATH_BINDING)
   USERID(apsp)                            UOWLOG(S0599317.LOG)
   UOWSTDA(2016-08-16)                     UOWSTTI(11.07.29)
   UOWLOGDA(2016-08-16)                    UOWLOGTI(11.07.29)
   URTYPE(QMGR)                         
   EXTURID(XA_FORMATID[] XA_GTRID[] XA_BQUAL[])

现在我希望我的输出显示

ABIS.CLIENT,101.54.151.80,malth,S0599315.LOG,11.06.38
APP.CLIENT,101.54.163.127,apsp,S0599317.LOG,11.07.29
SB.CLIENT,101.54.151.119,sbp,S0599317.LOG,11.07.38
CBM.CLIENT,101.54.163.37,machlth,S0599317.LOG,11.07.40

所以基本上,获取通道名称,conname,userid,UUOWLOG和UOWLOGTI,最后通过UOWLOGTI对它进行排序。 我试图在shell中这样做。

我尝试使用awk,但似乎无法获得所需的输出。

cat UOWLOG.txt|awk -vRS="" 'BEGIN{printf ""}/\<'AMQ8276'\>/^CHANNEL{printf $0 RT}' 当我在awk中尝试不同的选项时,我会收到错误。 请帮忙。

4 个答案:

答案 0 :(得分:2)

所有这些都可以在awk中完成,但使用一系列工具可能更容易

$ grep -oP '(CHANNEL|CONNAME|USERID|UOWLOG|UOWLOGTI)\(.*?\)' file | 
  awk -F'[()]' '{ORS=NR%5?",":RS; print $2}' | 
  sort -t, -k5

ABIS.CLIENT,101.54.151.80,malth,S0599315.LOG,11.06.38
APP.CLIENT,101.54.163.127,apsp,S0599317.LOG,11.07.29
SB.CLIENT,101.54.151.119,sbp,S0599317.LOG,11.07.38
U90785.CBM.CLIENT,101.54.163.37,machlth,S0599317.LOG,11.07.40

答案 1 :(得分:1)

我的解决方案与@ karakfa类似,但不对字段顺序做出任何假设:

$ grep -o '^\w\+:\|\w\+[(][^)]\+[)]' file |
  awk -F '[()]' -v OFS="," 'function printValues() { if ("CHANNEL" in p) { print p["CHANNEL"], p["CONNAME"], p["USERID"], p["UOWLOG"], p["UOWLOGTI"] } } /^\w+:/ { printValues(); delete p; next } { p[$1] = $2 } END { printValues() }' |
  sort -t, -k 5

我正在做的是为每个&#34;记录&#34;创建一个关联数组。然后只打印必要的字段。

答案 2 :(得分:1)

我有点脏gnu awk解决方案:

$ awk -v RS="AMQ" 'NR>1{
print gensub(/.*CHANNEL\(([^)]+).*CONNAME\(([^)]+).*USERID\(([^)]+).*UOWLOG\(([^)]+).*UOWLOGTI\(([^)]+).*/,"\\1,\\2,\\3,\\4,\\5","1")}' file | sort -t "," -k5
ABIS.CLIENT,101.54.151.80,malth,S0599315.LOG,11.06.38
APP.CLIENT,101.54.163.127,apsp,S0599317.LOG,11.07.29
SB.CLIENT,101.54.151.119,sbp,S0599317.LOG,11.07.38
U90785.CBM.CLIENT,101.54.163.37,machlth,S0599317.LOG,11.07.40

答案 3 :(得分:1)

我不知道,但是接受的答案并没有为&#34; CBM.CLIENT&#34;生成指定的输出。频道,到目前为止所有的答案都是强烈的单行。当变换如此复杂时,我喜欢整个脚本,所以我可以看到它是什么。

#! /usr/bin/awk -f 

function Print() {
    OFS = ","
    print line["CHANNEL"], line["CONNAME"], line["USERID"],     \
          line["UOWLOG"], line["UOWLOGTI"]
    delete line
}

/Display Connection details[.]$/ {
    if( length(line) ) {
        Print()
    }
    next
}

$1 == "CHANNEL" {
    line[$1] = $2
    if( 3 == split( $2, channel, /[.]/ ) ) {
        line[$1] = channel[2] "." channel[3]
    }
    next
}

{
    line[$1] = $2
}

END { Print() }

该技术是构建所有 foo(bar)元素的关联数组,并以明显的方式打印有趣的位。正则表达式很简单,并且放宽了输入假设:没有字段顺序依赖性,并且格式依赖性很小。

它依赖于一些预处理来将每个键/值对放在它自己的行上,并进行后处理以对输出进行排序。调用为:

$ cat f.sh
#! /bin/sh
sed -E 's/^ +//; s/ +$//g; s/  +/\
/g'  $1 | awk -F '[:()]' -f script.awk | sort -t, -k5

$ ./f.sh dat
ABIS.CLIENT,101.54.151.80,malth,S0599315.LOG,11.06.38
APP.CLIENT,101.54.163.127,apsp,S0599317.LOG,11.07.29
SB.CLIENT,101.54.151.119,sbp,S0599317.LOG,11.07.38
CBM.CLIENT,101.54.163.37,machlth,S0599317.LOG,11.07.40

在评论中,OP提到仅打印符合特定标准的行。这很容易添加到此Print()函数中,以后也很容易理解。