linux grep过滤_Linux中的Grep和过滤IP地址

本文介绍了如何在Linux环境中使用grep命令高效地过滤和提取文本文件或控制台输出中的IP地址,包括打印接口IP地址、仅显示IP地址等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

linux grep过滤

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}'
Print Interfaces IP Addresses
Print Interfaces IP Addresses
打印接口IP地址

仅打印I​​P地址(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)

Grep and Filter IP Address In Linux Infografic
Grep and Filter IP Address In Linux Infografic
Linux Infografic中的Grep和过滤IP地址
LEARN MORE  DHCP Reservation For Windows Server 2012, Windows Server 2008 and Benefits
了解更多有关Windows Server 2012,Windows Server 2008的DHCP保留和优点

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

linux grep过滤

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值