LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-11-2014, 04:15 PM   #1
threezerous
Member
 
Registered: Jul 2009
Posts: 89

Rep: Reputation: 15
grep for a string in nested string


I a not sure how to word my qq, so I will just post my example. I have a tnsnames.ora with multiple connection strings. In a shell script I am reading this tnsnames.ora. I need to get host of a specific SID. My tnsnames.ora looks like this. If I am passing the SID as a variable, how do I get host associated with that $sid?

Code:
SIDNAME1 =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = host_name_x.coompany.com)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = SIDNAME1)
    )
  )

SIDNAME2 =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = host_name_y.coompany.com)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = SIDNAME2)
    )
  )
 
Old 09-11-2014, 08:50 PM   #2
berndbausch
Senior Member
 
Registered: Nov 2013
Location: Tokyo
Distribution: Redhat/Centos, Ubuntu, Raspbian, Fedora
Posts: 1,687

Rep: Reputation: 353Reputation: 353Reputation: 353Reputation: 353
Quote:
Originally Posted by threezerous View Post
I a not sure how to word my qq, so I will just post my example. I have a tnsnames.ora with multiple connection strings. In a shell script I am reading this tnsnames.ora. I need to get host of a specific SID. My tnsnames.ora looks like this. If I am passing the SID as a variable, how do I get host associated with that $sid?

Code:
SIDNAME1 =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = host_name_x.coompany.com)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = SIDNAME1)
    )
  )

SIDNAME2 =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = host_name_y.coompany.com)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = SIDNAME2)
    )
  )
By SID, do you mean SIDNAME1 and SIDNAME2? If yes, grep won't help you much, since it works on single lines. Better write an awk program; off the cuff and not tested:
Code:
awk -v sid=$SID '/sid==$1/          { sidfound=1 }
                 /HOST/ && sidfound { sidfound=0
                                      sub("^.*HOST *= *","")
                                      sub(").*","")
                                      print }'
The intention of this script is to find the SID, set a flag that it was found, and then remove everything except for the host name from the line that contains the host name. I am sure there are much more elegant ways of doing it.

It seems to me, however, that your tnsnames.ora file can be formatted in many ways; e.g. the HOST statement could be spread over several lines. You probably need a parser generator like yacc to do this reliably. On the other hand, if you are certain that the file will always be in the format depicted above, awk does the job.
 
Old 09-11-2014, 09:32 PM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,842

Rep: Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823
TIMTOWTDI - even without using perl ...
You can get away without the flag if you read the entire "paragraph" in as one "record" - easy enough to do in this case with RS=''.
For the potentia of split names, I'd just toss (all) the \n away, but that might introduce other issues. That knowledge isn't available to us.

And, of course, perl can also handle this, but I agree grep isn't the right tool.
 
Old 09-12-2014, 11:04 AM   #4
threezerous
Member
 
Registered: Jul 2009
Posts: 89

Original Poster
Rep: Reputation: 15
Thanks for your suggestions and time taken to respond. I was able to make it work using the command

grep -A 5 $SID $file_tnsnames.ora | grep HOST | awk -F"HOST = " '{print $2}'|cut -d')' -f1
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Issue trying to grep a string, but keep a similar string Supp0rtLinux Linux - Software 7 10-04-2011 07:35 PM
[SOLVED] copy string a to string b and change string b with toupper() and count the chars beep3r Programming 3 10-22-2010 08:22 PM
[SOLVED] How to GREP string? tquang Linux - General 2 07-30-2010 01:51 AM
grep a particular string john83reuben Linux - Newbie 3 12-07-2009 10:34 AM
grep string with space (2 word string) casperdaghost Linux - Newbie 7 08-24-2009 03:11 AM


All times are GMT -5. The time now is 08:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration