Searching IP address in a text file or a console output may become cumbersome. This little command named grep will help you in this way.
Example Data
We have a file or output which includes the IP address and we want to extract just IP addresses nothing other. This file is created with a nmap scan.
Nmap scan report for 192.168.122.1 Host is up (0.00022s latency). Nmap scan report for kali (192.168.122.126) Host is up (0.00015s latency).
Print Interfaces IP Addresses
We can use ip addr
command which will print current system interfaces and related information. We can grep from this information and print only IP addresses currently the system is using with the following command.
$ ip add | grep -o -E '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'

Print Only IP Addresses
Here we use grep
command and give -o
option to only get the IP address. The default behavior of the grep
is printing lines which match given regex but if we only want to print matched text not the whole line we will use -o
option which will print only IP addresses.
$ grep -o -E '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' network_list.txt 192.168.122.1 192.168.122.126
Grep and Filter IP Address In Linux Infografic
