Shell – get parts of string using shell script

shellshell-scriptstring

I have one string as

/ip/192.168.0.1/port/8080/

I want to get two separate variables which will contain port and IP

like. 192.168.0.1 and 8080

as I know /ip/ and /port/ will be there always I got Ip as follows,

expr /ip/192.168.0.1/port/8080/ : '/ip/\(.*\)/port/' 

this will output 192.168.0.1
just don't know how to get port,
I tried similar command as,

expr /ip/192.168.0.1/port/8080/ : '/port/\(.*\)/' 

but it doesn't give port .. how to get port also.

Best Answer

You can use awk:

awk -F\/ '{print $2"="$3, $4"="$5}' input_file

with either an input file or just line by line.

Related Question