-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
  • 2,057
  • 2
  • 9
  • 22

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
  • 94,503
  • 21
  • 155
  • 181
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
  • 88,541
  • 4
  • 65
  • 105
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
  • 914
  • 6
  • 14
  • 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