Grep multiple pattern

I have got a text
from each line, I need to fetch port only if there is an ip

a.text text  and port=25
b.ip=(12.32.54.256) and port="52"
c.ip=(55.251.253.12) and port=25"
d.text text and port="5"
e.ip=(45.211.155.15) and port="457"
f.ip=(144.158.256.2) and port="588"

I know how to fetch ip per line:

awk 'BEGIN{ RS="<\n>"}1' file | grep -oP "(?<=ip=\()[^)]*" 

(well in this case, awk is not very usefull, but in the actual file, it is)

I know how to grab ports individually:

grep -oP "(?<=port=\")[^\"]*" file

but I do not know how to grab both and only both (line a and d should not be in the output)

Thanx in advance folks!

How about:

awk -F'port="?|"' '/ip=/{print $2}' file

Output:

52
25
457
588

--
Or:

sed -n 's/.*ip.*port="*\([^"]*\).*/\1/p' file

thanx Scrutinizer

awk -F'port="?|"' '/ip=/{print $2}' file

prints ports only.

OK, do you need ip as well ? How about:

awk -F'ip=[(]|[)]|port="?|"' '/ip=/{print $2,$4}' file

Output:

12.32.54.256 52
55.251.253.12 25
45.211.155.15 457
144.158.256.2 588

--
Or

sed -n 's/.*ip=(\([^)]*\).*port="*\([^"]*\).*/\1 \2/p' file

thanx again Scrutinizer!

both

awk -F'ip=[(]|[)]|port="?|"' '/ip=/{print $2,$4}' file

and

sed -n 's/.*ip=(\([^)]*\).*port="*\([^"]*\).*/\1 \2/p' file

work. (do you know how to do it with grep?)

Using GNU grep like you are using, and using its perl regex feature, I can see this possibility:

grep -oP '(?<=ip=\()[^)]*|((?<=\) and port=")|(?<=\) and port=))[^"]*' file

You need to use two alternate look behinds for port, since there may or may not be a double quote around the port number as your sample shows, and lookbehinds need to be fixed length.

But the ip and port will not be on the same line, so you would need something like:

grep -oP '(?<=ip=\()[^)]*|((?<=\) and port=")|(?<=\) and port=))[^"]*' file | xargs -n2

--
another option would be an extra grep stage:

grep ip= file | grep -oP '(?<=ip=\()[^)]*|((?<=port=")|(?<=port=))[^"]*' | paste - -

here shown with paste instead of xargs , which uses a TAB, which may be nicer

Thanx again Scrutinizer;
Your solution with grep is pretty cool.
To be honest, the missing -"- was a typo. it gave additional work. Sorry for that. Have a nice day!