Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
1  
Change the end of line like '.*/port/\(.*\)/' –  Costas Oct 31 at 10:01

7 Answers 7

up vote 3 down vote accepted

You can use awk:

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

with either an input file or just line by line.

share|improve this answer
    
Thank you :). I have edited question.. I only want 192.168.1.1 . not ip=192.168.1.1. –  Straw Hat Oct 31 at 10:02
    
Then remove the $2"=" and the $4"=" so it just prints the ip value and port value –  Isaac Oct 31 at 10:03
    
Got it :) thanks. –  Straw Hat Oct 31 at 10:06
    
Same with grep grep -o "[0-9.]\+" <<< /ip/192.168.0.1/port/8080/ –  Costas Oct 31 at 10:16

Another pure bash way using arrays:

$ s="/ip/192.168.0.1/port/8080/"        # initial string
$ a=(${s//// })                         # substitute / with " " and make array
$ echo ${a[1]}                          # Array index 1 (zero-based indexing)
192.168.0.1
$ echo ${a[3]}                          # Array index 3 (zero-based indexing)
8080
$ 

Or similar to the above, but using IFS instead of parameter expansion to split the string:

$ OLDIFS="$IFS"                         # save IFS
$ IFS="/"                               # temporarily set IFS 
$ a=($s)                                # make array from string, splitting on "/"
$ IFS="$OLDIFS"                         # restore IFS
$ echo "${a[2]}"                        # Array index 2
192.168.0.1
$ echo "${a[4]}"                        # Array index 4
8080
$ 

Note this method is potentially more general than the other two in this answer in that it should still work if the fields of interest contain whitespace.


Or using positional parameters:

$ s="/ip/192.168.0.1/port/8080/"        # initial string
$ set -- ${s//// }                      # substitute / with " " and assign params
$ echo $2                               # Param 2
192.168.0.1
$ echo $4                               # Param 4
8080
$ 
share|improve this answer

You can simply use cut as follows:

cut -d '/' -f 3,5

Example:

$ echo '/ip/192.168.0.1/port/8080/' | cut -d '/' -f 3,5
192.168.0.1/8080

This will cut with delimiter / and prints 3rd and 5th fields.

Or following may you want:

$ echo ip=`cut -d '/' -f 3 input_file` port=`cut -d '/' -f 5 input_file`
ip=192.168.0.1 port=8080
share|improve this answer

With bash

s=/ip/192.168.0.1/port/8080/
IFS=/ read -r _ _ ip _ port <<<"$s"
echo "$ip"
192.168.0.1
echo "$port"
8080
share|improve this answer
expr /ip/192.168.0.1/port/8080/ : '.*/port/\(.*\)/'

.* matches the initial part of the string before /port

share|improve this answer
    
yes it works. thanks. don't know Regex that much :( –  Straw Hat Oct 31 at 10:04

This is another way

$ cut -d '/' -f 3,5 <<< "/ip/192.168.0.1/port/8080/"|tr -s '/' ' '
192.168.0.1 8080
share|improve this answer
    
"I want to get two separate variables ..." –  G-Man Oct 31 at 17:49
    
Ops, missed it. In that case @DigitalTrauma answers should be the appropriate one. –  Kannan Mohan Oct 31 at 17:55

perl solutions:

echo /ip/192.168.0.1/port/8080/ | perl -F'/' -nae 'print "ip: ".$F[2]." port: ".$F[4]."\n"'

OUTPUT:

ip: 192.168.0.1 port: 8080
share|improve this answer
    
The question says, "I want to get two separate variables ...". Your answer doesn't actually accomplish that. –  G-Man Oct 31 at 18:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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