awk在shell中打印数组的一部分

时间:2016-05-02 15:30:45

标签: shell awk

您好我有一个包含数百行的文本文件。这是一个样本。

Array 1 - JVMAZURE,/www/deploy/DEV/JVMAZURE/ReceiveResponseFromAzureDEV,com.azure.ReceiveResponseFromAzure

JVM - JVMAZURE
Node - com.azure.ReceiveResponseFromAzure
Jar - /www/deploy/DEV/JVMAZURE/ReceiveResponseFromAzureDEV
AXIOM.SVAP.IN
LOG.XML.INPUT
SERVADMIN.ERROR
SERVADMIN.INPUT
TIMEOUT.IN
---------------------------------------------------------------------------------------------

Array 2 - JVMAZURE,/www/deploy/DEV/JVMAZURE/ReceiveResponse,com.azure.RecivedResponse

JVM - JVMAZURE
Node - com.azure.RecivedResponse
Jar - /www/deploy/DEV/JVMAZURE/ReceiveResponseDEV
AXIOM.SVAP.IN
EXCEPTION.QUEUE
SERVADMIN.ERROR
SERVADMIN.INPUT
---------------------------------------------------------------------------------------------

Array 3 - JVMAZURE1,/www/deploy/DEV/JVMAZURE1/SendMessageToAzureCloudDEV,com.azure.SendMessageToAzureCloud

JVM - JVMAZURE1
Node - com.azure.SendMessageToAzureCloud
Jar - /www/deploy/DEV/JVMAZURE1/SendMessageToAzureCloudDEV
AZURE.SVAP.IN
LOG.XML.INPUT
AXIOM.SERVADMIN.OUTPUT
SERVADMIN.ERROR
SB.OUT
TIMEOUT.IN
---------------------------------------------------------------------------------------------

Array 4 - JVMAZURE2,/www/deploy/DEV/JVMAZURE2/SendMessageFromAzureCloudDEV,com.azure.SendMessageFromAzureCloud

JVM - JVMAZURE2
Node - com.azure.SendMessageFromAzureCloud
Jar - /www/deploy/DEV/JVMAZURE2/SendMessageFromAzureCloudDEV
AZURE.SVAP.IN
SERVADMIN.ERROR
TIMEOUT.IN
---------------------------------------------------------------------------------------------

我正在尝试grep或awk下面的Jar下面的行/模式,并尝试打印JVM,Node和我正在搜索的那一行。

所以,如果我正在搜索AXIOM.SVAP.IN,我希望输出为

JVMAZURE,com.azure.ReceiveResponseFromAzure,AXIOM.SVAP.IN
JVMAZURE,com.azure.RecivedResponse,AXIOM.SVAP.IN

或者如果我正在搜索SERVADMIN.ERROR我想要输出

  JVMAZURE,com.azure.ReceiveResponseFromAzure,SERVADMIN.ERROR
  JVMAZURE,com.azure.RecivedResponse,SERVADMIN.ERROR
  JVMAZURE1,com.azure.SendMessageToAzureCloud,SERVADMIN.ERROR
  JVMAZURE2,com.azure.SendMessageFromAzureCloud,SERVADMIN.ERROR

我尝试过使用awk但没有得到任何一致的结果。

cat AZURE.txt|awk -v OFS=, -v r="^'|'$" '/SERVADMIN.ERROR/ { p=$NF; gsub(r,"",p) } /EG/ { e=$NF } /Flow/ {f=$NF; gsub(r,"",f); print e,f,p }'

和我在一起

JVMAZURE,com.azure.ReceiveResponseFromAzure,
JVMAZURE,com.azure.RecivedResponse,SERVADMIN.ERROR
JVMAZURE1,com.azure.SendMessageToAzureCloud,SERVADMIN.ERROR
JVMAZURE2,com.azure.SendMessageFromAzureCloud,SERVADMIN.ERROR

如果我搜索AXIOM.SVAP.IN

,行/模式的结果也会改变

1 个答案:

答案 0 :(得分:2)

每当输入中有name->值对时,最好创建一个数组(下面为f[])来保存该关系,然后您只需按名称访问这些值:

$ cat tst.awk
BEGIN { FS=" - "; OFS="," }
(Jar in f) && ($0 == tgt) { print f["JVM"], f["Node"], $0 }
{ f[$1] = $2; prev = $1 }
/^-+$/ { delete f }

$ awk -v tgt='AXIOM.SVAP.IN' -f tst.awk file
JVMAZURE,com.azure.ReceiveResponseFromAzure,AXIOM.SVAP.IN
JVMAZURE,com.azure.RecivedResponse,AXIOM.SVAP.IN

$ awk -v tgt='SERVADMIN.ERROR' -f tst.awk file
JVMAZURE,com.azure.ReceiveResponseFromAzure,SERVADMIN.ERROR
JVMAZURE,com.azure.RecivedResponse,SERVADMIN.ERROR
JVMAZURE1,com.azure.SendMessageToAzureCloud,SERVADMIN.ERROR
JVMAZURE2,com.azure.SendMessageFromAzureCloud,SERVADMIN.ERROR