sed (parsing value)

All,

Can somebody provide me with some sed expertise on how to parse the
following line.

27-MAR-2011 10:28:01 * (CONNECT_DATA=(SID=dmart)(CID=(PROGRAM=sqlplus)(HOST=mtasnprod1)(USER=mtasnord))) * (ADDRESS=(PROTOCOL=tcp)(HOST=10.197.7.47)(PORT=54881)) * establish * dmart * 0

I would like 3 different sed commands just in case.

1) get the first occurance of HOST= this should return mtasnprod1

2) get the last occurance of HOST = this should return 10.197.7.47

3) get all occurance of HOST = from each line tihs should return
mtasnprod1
10.197.7.47

Please note multiple occurances should be on different lines.

Thanks in advance to all who answer.

I have the following line, which I am trying to parse and am wondering if somebody cab pro

Does it have to be sed?

echo '27-MAR-2011 10:28:01 * (CONNECT_DATA=(SID=dmart)(CID=(PROGRAM=sqlplus)(HOST=mtasnprod1)(USER=mtasnord))) * (ADDRESS=(PROTOCOL=tcp)(HOST=10.197.7.47)(PORT=54881)) * establish * dmart * 0' | nawk -F'HOST=' '{for(i=2;i<=NF;i++) print substr($i,1,index($i,")")-1)}'

You can modify the above to get all of your 3 cases.

1 Like
# cat tst
27-MAR-2011 10:28:01 * (CONNECT_DATA=(SID=dmart)(CID=(PROGRAM=sqlplus)(HOST=mtasnprod1)(USER=mtasnord))) * (ADDRESS=(PROTOCOL=tcp)(HOST=10.197.7.47)(PORT=54881)) * establish * dmart * 0
# sed 's/^.*HOST=\([^)]*\)).*HOST=\([^)]*\)).*$/\1 \2/' tst
mtasnprod1 10.197.7.47
# sed 's/HOST/#/g;s/[^#]*#=//1;s/).*//' tst
mtasnprod1
# sed 's/.*HOST=//;s/).*//' tst
10.197.7.47

---------- Post updated at 06:16 PM ---------- Previous update was at 06:06 PM ----------

# sed 's/HOST=/#/g;s/^[^#]*#//;s/).*#/\n/;s/).*//' tst
mtasnprod1
10.197.7.47

---------- Post updated at 06:23 PM ---------- Previous update was at 06:16 PM ----------

# awk -v RS="[()]" -v FS="=" '$1=="HOST"{print $2}' tst
mtasnprod1
10.197.7.47

---------- Post updated at 06:29 PM ---------- Previous update was at 06:23 PM ----------

or (if your RS does not suport multi char def)

# nawk -v RS='(' -F"[=)]" '$1=="HOST"{print $2}' tst
mtasnprod1
10.197.7.47

ctsgnb if there were another thumbs up button I would give it to you too. Thanks for your eloquent solution.