parse tnsnames.ora using grep to extract hostname minus the domain
Question
I have tnsnames.ora file like
DB_CONNECTION1=
(description=
(address=
(protocol=tcp)
(host=myhost1.mydomain.com)
(port=1234)
)
(connect_data=
(sid=ABCD)
(sdu=4321)
)
DB_CONNECTION2=
(description=
(address=
(protocol=tcp)
(host=myhost2.mydomain.com)
(port=1234)
)
(connect_data=
(sid=ABCD)
(sdu=4321)
)
What regular expression do i need to use to extract the value myhost from the key host.
Ouput should be
myhost1
myhost2
Solution
As grep prints the whole line, you could do it in steps like:
grep "(host=" tnsnames.ora | cut -f 2 -d '=' | cut -f 1 -d '.'
To break it down:
- grep "(host=" tnsnames.ora -- finds all lines with the entry "(host="
- cut -f 2 -d '=' -- finds what is in column number 2 if you divide by the '=' character
- cut -f 1 -d '.' -- finds what is in column number 1 if you divide by the '.' character
You can execute the chain of commands to any point, to see the intermediate result, like:
grep "(host=" tnsnames.ora | cut -f 2 -d '='
Would give you:
myhost1.mydomain.com)
myhost2.mydomain.com)
That way it's easy to build your set of commands to do this type of thing.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow