I have a requirement to extract the database version in order to fulfill a script requirement.

I need to fetch the db details from oratab and its version.

Suppose my entry in oratab as below :


Here I need to extract only the db versions like 11 or 9 for a particular database. Which means that the shell script should only display 11 or 9.

  • Does the answer below solve your problem? If not, please clarify; if so, please use the checkmark to indicate that to the system. Thank you!
    – Jeff Schaller
    May 27 '16 at 15:25

If your version of awk supports regular expressions as the field separator, you can do this:

$ awk -F'[/.]' '{print $5}' oratab

alternatively, if you want the dbname printed too:

$ awk -F'[:/.]' '{print $1,$6}' oratab 
dbname1 11
dbname2 9

I'm not sure if the Solaris or AIX versions of awk supports regexps as field separator. GNU awk does. So does FreeBSD's version. mawk and original-awk do too.

This should work on Solaris awk (should work on any awk):

awk -F'/' '/^#/ {next}; /kmad/ {sub(/\..*/,"",$5);print $5}' /etc/oratab

It skips lines beginning with a # comment, and then (for all lines containing kmad) it strips everything from the first . onwards from field 5, and then prints that field.

And if you've got some really weird awk that doesn't understand either regexps in the field-separator or the next statement, try this:

awk -F'/' '! /^#/ && /kmad/ {sub(/\..*/,"",$5);print $5}' /etc/oratab

and if your awk doesn't have sub() either, then it's not awk, it's a dodo. Bury it and get a real awk.

  • Comments are not for extended discussion; this conversation has been moved to chat.
    – terdon
    May 26 '16 at 13:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.