-1

I have a string of IP and port numbers which is like : "10.213.110.49.33482;10.213.106.12.20001:" The two ip's are separated by a semi-colon. Last decimal shows port number.
What I need is to convert the above string which would look like: "10.213.110.49 20001:".
I want to remove the middle octets and port number of first IP using SHELL SCRIPT only. I want to extract the IP from first part ignoring the port number and from second IP i want the port number only

Prashant Kumar
  • 1,770
  • 2
  • 7
  • 20

3 Answers3

1

using sed:

sed 's/\.[0-9]*;.*\.\([0-9]*:\)/ \1/' <<< \
         '10.213.110.49.33482;10.213.106.12.20001:'

gives:

10.213.110.49 20001:
perreal
  • 90,214
  • 20
  • 145
  • 172
0

awk solution:

s="10.213.110.49.33482;10.213.106.12.20001:"
echo $s | awk -F"[.;]" '{OFS=".";print $1,$2,$3,$4" "$10}'

The output:

10.213.110.49 20001:
RomanPerekhrest
  • 75,918
  • 4
  • 51
  • 91
0

You can just use bash substitution, just takes a couple of steps:

s='10.213.110.49.33482;10.213.106.12.20001:'

port="${s##*.}"

ip="${s%;*}"
ip="${ip%.*}"

echo "$ip $port"
grail
  • 906
  • 6
  • 13
  • Thanks a lot @grail. u did exactly what i needed. – Prashant Kumar Apr 05 '17 at 06:51
  • Can u please explain what you did here. – Prashant Kumar Oct 05 '17 at 06:09
  • # removes from the start and % removes from the end. If you double either it removes from the same direction until the last occurrence. So ${s#*.} would remove from the front up until first period, but ${s##*.} would remove all up until the last period in the string – grail Oct 05 '17 at 11:49