获取由shell

时间:2016-11-01 19:55:27

标签: shell awk

我有大约100行的文本文件。这是一个例子。

2.2  1.0 Flow DEV1 f06c6ba8-5201-0000-0080-a6ced6fdded7 CAPPA
0.9  2.2 Flow DEV1 7d702aa8-5201-0000-0080-dff8707d04a5 PART
0.7  1.6 Flow DEV1 f19d2aa8-5201-0000-0080-e4f1da3f1eb7 PARTREL
0.7  1.5 Flow DEV1 67cc2aa8-5201-0000-0080-e1e7bf540f80 PARTTRAIN
0.6  2.9 Flow DEV1 8bc628a8-5201-0000-0080-bbf00e3c6b25 CUSTOMER
0.6  1.6 Flow DEV1 344229a8-5201-0000-0080-ca7e456e1a13 GENERAL
0.4  2.3 Flow DEV1 1cfe2ca8-5201-0000-0080-8039b85b1161 DEALER
0.4  1.7 Flow DEV1 3f552ba8-5201-0000-0080-d17d8da987d3 LINK3
0.3  1.7 Flow DEV1 50dd2ba8-5201-0000-0080-e392a91798c5 LINKREL
0.3  1.5 Flow DEV1 c60e2ca8-5201-0000-0080-f640a9c17a0f LINKTRAIN

我正在尝试输出

was,host=server1,environment=DEVL,jvm=DEV1,objecttype=process,processname=CAPPA,%CPU=2.2,%MEM=1.0
was,host=server1,environment=DEVL,jvm=DEV1,objecttype=process,processname=PART,%CPU=0.9,%MEM=2.2
was,host=server1,environment=DEVL,jvm=DEV1,objecttype=process,processname=PARTREL %CPU=0.7,%MEM=1.6
was,host=server1,environment=DEVL,jvm=DEV1,objecttype=process,processname=PARTTRAIN %CPU=0.7,%MEM=1.5
was,host=server1,environment=DEVL,jvm=DEV1,objecttype=process,processname=CUSTOMER %CPU=0.6,%MEM=2.9
was,host=server1,environment=DEVL,jvm=DEV1,objecttype=process,processname=GENERAL %CPU=0.6,%MEM=1.6
was,host=server1,environment=DEVL,jvm=DEV1,objecttype=process,processname=DEALER %CPU=0.4,%MEM=2.3
was,host=server1,environment=DEVL,jvm=DEV1,objecttype=process,processname=LINK3 %CPU=0.4,%MEM=1.7
was,host=server1,environment=DEVL,jvm=DEV1,objecttype=process,processname=LINKREL %CPU=0.3,%MEM=1.7
was,host=server1,environment=DEVL,jvm=DEV1,objecttype=process,processname=LINKTRAIN %CPU=0.3,%MEM=1.5

我尝试使用awk并剪切但没有运气。我做错了什么?

awk -v jvm="DEV1" -v hstnm="server1" -v environment="DEVL" '
  {
    gsub(/ /, "", $1)
    gsub(/ /, "", $2)
    gsub(/ /, "", $6)
    print    "was,host="                         hstnm       \
             ",environment="                     environment \
             ",jvm="                             jvm         \
             ",objecttype=process,processname="  $6          \
             " %CPU="                            $1          \
             ",%MEM="                            $2          \
  }' FIELDWIDTHS="11 7 11" < stats.txt

1 个答案:

答案 0 :(得分:2)

awk在空白区域为您解析,如果您有主要想法,则用变量替换硬编码值。

$ awk '{print "was,host=server1,environment=DEVL,jvm=" $4 
              ",objecttype=process,processname=" $6 ",%CPU=" $1 ",%MEM=" $2}' file

was,host=server1,environment=DEVL,jvm=DEV1,objecttype=process,processname=CAPPA,%CPU=2.2,%MEM=1.0
was,host=server1,environment=DEVL,jvm=DEV1,objecttype=process,processname=PART,%CPU=0.9,%MEM=2.2
was,host=server1,environment=DEVL,jvm=DEV1,objecttype=process,processname=PARTREL,%CPU=0.7,%MEM=1.6
was,host=server1,environment=DEVL,jvm=DEV1,objecttype=process,processname=PARTTRAIN,%CPU=0.7,%MEM=1.5
was,host=server1,environment=DEVL,jvm=DEV1,objecttype=process,processname=CUSTOMER,%CPU=0.6,%MEM=2.9
was,host=server1,environment=DEVL,jvm=DEV1,objecttype=process,processname=GENERAL,%CPU=0.6,%MEM=1.6
was,host=server1,environment=DEVL,jvm=DEV1,objecttype=process,processname=DEALER,%CPU=0.4,%MEM=2.3
was,host=server1,environment=DEVL,jvm=DEV1,objecttype=process,processname=LINK3,%CPU=0.4,%MEM=1.7
was,host=server1,environment=DEVL,jvm=DEV1,objecttype=process,processname=LINKREL,%CPU=0.3,%MEM=1.7
was,host=server1,environment=DEVL,jvm=DEV1,objecttype=process,processname=LINKTRAIN,%CPU=0.3,%MEM=1.5