Search code examples
shellunixsh

Get domain home / base address from url with IP and Port in shell script


Have inputs like

url1='http://10.25.225.123:32782/actuator/health' url2='http://10.25.225.321:12345/myappmanagement/health' and so on.

How to get only http://10.25.225.123:32782/ and http://10.25.225.321:12345 for url1 and url2 respectively using the same command?


Solution

  • url='http://10.25.225.123:32782/actuator/health'
    awk -F'/' 'BEGIN{OFS=FS="/"} {print $1,$2,$3}' <<< "$url"
    http://10.25.225.123:32782
    

    or simply:

    cut -d/ -f-3 <<< "$url"
    http://10.25.225.123:32782