user3164754 user3164754 - 1 month ago 7
Bash Question

get values separated by random spaces in lines from text file in shell

I have text file with about a 100 lines. Here's an example.

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


I am trying to get an output as

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


I tried using awk and cut but no luck. What am i doing wrong?

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

Answer

awk does the parsing for you on white space, replace hard coded values with variables if you got the main idea.

$ 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