
linux grep过滤
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.
在文本文件或控制台输出中搜索IP地址可能很麻烦。 这个名为grep的小命令将以这种方式帮助您。
示例数据 (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.
我们有一个包含IP地址的文件或输出,我们只想提取IP地址即可。 该文件是使用nmap扫描创建的。
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).
打印接口IP地址 (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 addr
命令来打印当前系统界面和相关信息。 我们可以根据以下信息grep并仅打印系统当前正在使用的IP地址。
$ ip add | grep -o -E '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'

仅打印IP地址(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
选项仅获取IP地址。 grep
的默认行为是打印与给定正则表达式匹配的行,但是如果我们只想打印匹配的文本而不是整行,我们将使用-o
选项,该选项仅打印IP地址。
$ 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
Linux Infografic中的Grep和过滤IP地址 (Grep and Filter IP Address In Linux Infografic)

翻译自: https://www.poftut.com/grep-and-filter-ip-address-in-linux/
linux grep过滤