-2
@(description=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=211.67.48.11)(PORT=1521))(ADDRESS=(PROTOCOL=TCP)(HOST=211.67.48.12)(PORT=1521)))(FAILOVER=yes)(LOAD_BALANCE=yes)(CONNECT_DATA =(SERVER = DEDICATED)(SERVICE_NAME = YWKDB)))"

above is the oracle connect str, I want to use grep or sed or awk to get all IP and PORT info, like 211.67.48.11:1521,211.67.48.12:1521 and sidname:YWKDB

the str is in TOMCAT_HOME/conf/context.xml, I need to get the IP info.

jkzhao
  • 3
  • 3
  • 2
    Possible duplicate of [How do you extract IP addresses from files using a regex in a linux shell?](https://stackoverflow.com/questions/427979/how-do-you-extract-ip-addresses-from-files-using-a-regex-in-a-linux-shell) – Wiktor Stribiżew Mar 29 '19 at 08:55
  • I also want to get port and sid info,thanks – jkzhao Mar 29 '19 at 09:25

1 Answers1

1

Comments in code.

# The input. Sorry cat. 
cat <<'EOF' |
@(description=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=211.67.48.11)(PORT=1521))(ADDRESS=(PROTOCOL=TCP)(HOST=211.67.48.12)(PORT=1521)))(FAILOVER=yes)(LOAD_BALANCE=yes)(CONNECT_DATA =(SERVER = DEDICATED)(SERVICE_NAME = YWKDB)))
EOF
{
        # substitute `(` for newlines
        tr '(' '\n' |
        # substitue `)` for newlines
        tr ')' '\n' |
        # now that we have all in newlines, we can filter HOST and PORT lines
        grep 'HOST\|PORT' |
        # remove the HOST and PORT part before `=`
        sed 's/[^=]*=//' |
        # read two lines at a time
        while read host && read port; do
            # some output
            echo "HOST=$host PORT=$port"
        done
}

will output:

HOST=211.67.48.11 PORT=1521
HOST=211.67.48.12 PORT=1521
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • Your answer is useful, although I understand hard, thanks! Could you help add service_name info? – jkzhao Mar 29 '19 at 09:28
  • 1
    I got it, thanks! `echo $url_info | tr '(' '\n' |tr ')' '\n' | grep 'HOST\|PORT\|SERVICE_NAME'` – jkzhao Mar 29 '19 at 09:59