Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to find a specific IP address by grep regular expression

Shulou Source: shulou.com Published: 2022-06-01 15:50:12 09月30日 Update

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!

Tags: Address regular expression code face command content file learning practical deeper interest just can pass character string practicality practical simple operation method Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Shulou Tech Info Apple vpn macOS