sed (parsing value)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed (parsing value)
# 1  
Old 03-28-2011
sed (parsing value)

All,

Can somebody provide me with some sed expertise on how to parse the
following line.
Code:
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
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 03-28-2011 at 12:44 PM.. Reason: please use code tags!
# 2  
Old 03-28-2011
Does it have to be sed?
Code:
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.

Last edited by vgersh99; 03-28-2011 at 01:08 PM..
This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 03-28-2011
Code:
# 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

Code:
# sed 's/^.*HOST=\([^)]*\)).*HOST=\([^)]*\)).*$/\1 \2/' tst
mtasnprod1 10.197.7.47

Code:
# sed 's/HOST/#/g;s/[^#]*#=//1;s/).*//' tst
mtasnprod1

Code:
# sed 's/.*HOST=//;s/).*//' tst
10.197.7.47

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

Code:
# 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 ----------

Code:
# 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)
Code:
# nawk -v RS='(' -F"[=)]" '$1=="HOST"{print $2}' tst
mtasnprod1
10.197.7.47


Last edited by ctsgnb; 03-28-2011 at 01:36 PM..
# 4  
Old 03-28-2011
ctsgnb if there were another thumbs up button I would give it to you too. Thanks for your eloquent solution.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parsing via sed issue

sorry I messed up the last post with too many mistakes and corrections so I closed it and opening a new one which should be clear to everyone .my apologies to the admins. I am using sun solaris and Linux , what I want is SED to print any string (or output it to a file preferably) that does... (2 Replies)
Discussion started by: boncuk
2 Replies

2. UNIX for Dummies Questions & Answers

sed or Grep Parsing

I would like to parse two strings from lines in a file only when both strings appear on the same line. For example, if I have the following line: string1 string2 string3 string4 string5 string6 string7 string8 string9 I would like the output to be: string2: string7 Can someone give me... (5 Replies)
Discussion started by: ARBlue79
5 Replies

3. Shell Programming and Scripting

[SED] Parsing to get a single value

Hello guys, I guess you are fed up with sed command and parse questions, but after a while researching the forum, I could not get an answer to my doubt. I know it must be easy done with sed command, but unfortunately, I never get right syntax of this command OK, this is what I have in my... (3 Replies)
Discussion started by: manolain
3 Replies

4. Shell Programming and Scripting

Sed special parsing

What is the shortest & right way to remove the string "" with a sed statement ? echo 'whateverwhatever' | sed ........ ? :) (2 Replies)
Discussion started by: ctsgnb
2 Replies

5. Shell Programming and Scripting

Parsing cron with sed

Hello I want to convert my cron list into a csv Can you please help me with sed ? eg: Convert #06,21,36,51 * * 1,2 * (. ~/.profile ; timex /some/path/script -30 -15) >> /some/path/logfile2 2>&1 * * * * * (. ~/.profile ; timex /some/path/script2) > /some/path/logfile2 To:... (1 Reply)
Discussion started by: drbiloukos
1 Replies

6. Shell Programming and Scripting

Parsing with awk or sed

I want to delete corrupt records from a file through awk or sed. Can anyone help me with this Thanks Striker Change subject to a descriptive one, ty. (1 Reply)
Discussion started by: Rahul_us
1 Replies

7. Shell Programming and Scripting

sed selective data parsing

i have file in the following format *RECORD* *FIELD NO* 123456 *FIELD TX* this is a sample entry *FIELD SA* See Also *FIELD RF* References *FIELD CS* Clinical Symptoms *FIELD AV* Allelic Variants *FIELD EH* Edit History *RECORD* *FIELD NO* 123456 (1 Reply)
Discussion started by: dunstonrocks
1 Replies

8. Shell Programming and Scripting

Sed parsing error

I'm having a problem with a sed script. A programmer needs to change columns 942,943,944 to blank spaces only where it has the number 999 in every line. I didn't have a copy of the data file to test with originally so made my own up with a bunch of x's and put 999 in columns 5-7. The sed... (1 Reply)
Discussion started by: gravy26
1 Replies

9. Shell Programming and Scripting

awk sed parsing

hi , i would like to parse some file with the fallowing data : data data data "unwanted data" data data "unwanted data" data data data data #unwanted data. what i want it to have any coments between "" and after # to be erased using awk or/and sed. has anyone an idea? thanks. (3 Replies)
Discussion started by: Darsh
3 Replies

10. UNIX for Dummies Questions & Answers

shell script parsing with sed

#I'm quite new to scripting and my boss has asked me to solve a simple problem and sadly, I can't figure out how to do it. Any help is appreciated. :confused: #The following is a small shell script and the output that it produces for google.com. #!/bin/sh whois $1 | grep "Name Server"... (5 Replies)
Discussion started by: jjamd64
5 Replies
Login or Register to Ask a Question