Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to find a specific IP address by grep regular expression

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article focuses on "how to find a specific IP address by grep regular expression". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to find a specific IP address with grep regular expressions.

Here are a few examples of using regular expressions in the grep command to match from a file to a specific IP address. The following regular expression will match the address of IPV4.

The regular expression matches the IP address:

Use the following regular expression to match the address of IPV4, starting with the pattern from 0.0.0.0 to 999.999.999.999.

The code is as follows:

"([0-9] {1jue 3} [\.]) {3} [0-9] {1jue 3}"

Example: use the grep command to find a match for a specific address

The code is as follows:

$grep-E-o "([0-9] {1jue 3} [\.]) {3} [0-9] {1pm 3}" filename

[root@devops ~] # echo 000.333.222.444 > test1

[root@devops ~] #

[root@devops ~] # grep-E-o "([0-9] {1jue 3} [\.]) {3} [0-9] {1Jue 3}" test1

000.333.222.444

The above regular expression simply matches a specific pass similar to an ip address, but it does not match the real IPv4 address. It can be matched by the following regular expression.

The code is as follows:

"(25 [0-5] | 2 [0-4] [0-9] | [01]? [0-9] [0-9]?)\. (25 [0-5] | 2 [0-4] [0-9] | [01]? [0-9] [0-9]?)\. (25 [0-5] | 2 [0-4] [0-9] | [01]? [0-9] [0-9]?).

(25 [0-5] | 2 [0-4] [0-9] | [01]? [0-9] [0-9]?) "

Use the grep command to match the IPv4 address in the file

The code is as follows:

[root@devops ~] # grep-E-o "(25 [0-5] | 2 [0-4] [0-9] | [01]? [0-9] [0-9]?)\. (25 [0-5] | 2 [0-4] [0-9] | [01]? [0-9] [0-9]?)\ (25 [0-5] | 2 [0-5]

4] [0-9] | [01]? [0-9] [0-9]?)\. (25 [0-5] | 2 [0-4] [0-9] | [01]? [0-9] [0-9]?) "test1

[root@devops ~] # echo 192.168.1.234 > test1

[root@devops ~] # grep-E-o "(25 [0-5] | 2 [0-4] [0-9] | [01]? [0-9] [0-9]?)\. (25 [0-5] | 2 [0-4] [0-9] | [01]? [0-9] [0-9]?)\ (25 [0-5] | 2 [0-5]

4] [0-9] | [01]? [0-9] [0-9]?)\. (25 [0-5] | 2 [0-4] [0-9] | [01]? [0-9] [0-9]?) "test1

192.168.1.234

[root@devops]

#

Where the-E option represents a regular expression that uses the grep extension

The-o option displays only matching strings

At this point, I believe you have a deeper understanding of "how to find a specific IP address with grep regular expressions". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report