get parts of string using shell script

23,211

Solution 1

You can use awk:

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

with either an input file or just line by line.

Solution 2

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

Solution 3

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
$ 

Solution 4

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

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

Solution 5

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:
23,211

Related videos on Youtube

stormy
Author by

stormy

Updated on September 18, 2022

Comments

  • stormy
    stormy almost 2 years

    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.

    • Costas
      Costas over 9 years
      Change the end of line like '.*/port/\(.*\)/'
  • stormy
    stormy over 9 years
    Thank you :). I have edited question.. I only want 192.168.1.1 . not ip=192.168.1.1.
  • Isaac
    Isaac over 9 years
    Then remove the $2"=" and the $4"=" so it just prints the ip value and port value
  • stormy
    stormy over 9 years
    yes it works. thanks. don't know Regex that much :(
  • Costas
    Costas over 9 years
    Same with grep grep -o "[0-9.]\+" <<< /ip/192.168.0.1/port/8080/
  • G-Man Says 'Reinstate Monica'
    G-Man Says 'Reinstate Monica' over 9 years
    "I want to get two separate variables ..."
  • Kannan Mohan
    Kannan Mohan over 9 years
    Ops, missed it. In that case @DigitalTrauma answers should be the appropriate one.