4

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.

  • 1
    Change the end of line like '.*/port/\(.*\)/' – Costas Oct 31 '14 at 10:01
3

You can use awk:

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

with either an input file or just line by line.

| 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 '14 at 10:02
  • Then remove the $2"=" and the $4"=" so it just prints the ip value and port value – Isaac Oct 31 '14 at 10:03
  • Same with grep grep -o "[0-9.]\+" <<< /ip/192.168.0.1/port/8080/ – Costas Oct 31 '14 at 10:16
6

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
| improve this answer | |
4

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
$ 
| improve this answer | |
3
expr /ip/192.168.0.1/port/8080/ : '.*/port/\(.*\)/'

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

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

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
| improve this answer | |
0

This is another way

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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