How to extract port number?

Hi my code is as follow:

stringA=`cat /s01/oracle/11.2/network/admin/listener.ora | grep "PORT"`
stringB=`cat $ORACLE_HOME/network/admin/listener.ora | grep "PORT"`
stringC="PORT"

echo ${stringA}
echo ${stringB}
echo ${stringC}

position=`expr index "$stringB" "stringC"`

echo ${position}

output is as follow

(DESCRIPTION_LIST =(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=SG-RH5.3-64)(PORT=1522)))
(DESCRIPTION_LIST =(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=SG-RH5.3-64)(PORT=1522)))
PORT
7


But unfortunately I don't understand why position 7 is return and if position 7 is returned, I definitely cannot extract 1522 as port number, i.e. $port_number=1522

I'm running on Redhat 5.3

Red Hat Enterprise Linux Server release 5.3 (Tikanga)

thanks

Here is another approach:

stringB="(DESCRIPTION_LIST =(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=SG-RH5.3-64)(PORT=1522)))"
stringB=${stringB##*=}
stringB=${stringB%%)*}
echo "Port Number = $stringB"

OR

stringB=${stringB##*PORT=}
stringB=${stringB%%)*}
1 Like

Another way to get the port is by using grep -o :

grep -o PORT=[0-9]* file

Or by using perl:

perl -ne '/PORT=([0-9]*)/; print $1' file