# grep ip address from file

**URL:** <https://community.unix.com/t/grep-ip-address-from-file/309722>\
**Category:** Shell Programming and Scripting\
**Created:** [May 7, 2012, 4:18am UTC](https://community.unix.com/t/grep-ip-address-from-file/309722 "2012-05-07T04:18:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![anil510](https://community.unix.com/letter_avatar/anil510/32/6_449c533f586939b6e5f246632a46421f.png) [@anil510](https://community.unix.com/u/anil510)\
**Post date:** [May 7, 2012, 4:18am UTC](https://community.unix.com/t/grep-ip-address-from-file/309722/1 "2012-05-07T04:18:05Z")

</div>

I have a file

```nohighlight
[me@server hi]$ cat ip12
11.22.33.44
192.68.1.2
helo
l
72.34.34.200
333.444.555.666
12.23e.544.423
myip1 11.22.33.44
myip2 33.44.55.66 #fine this IP should also be listed

```

I do

```nohighlight
[me@server hi]$ cat ip12 | grep '^[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}$'
11.22.33.44
192.68.1.2
72.34.34.200
333.444.555.666

```

I get results of IP address, but I need here 11.22.33.44 ad 33.44.55.66 also listed.

---------- Post updated at 03:18 AM ---------- Previous update was at 03:15 AM ----------

Nevermind I got it

```nohighlight
[me@server me]$ cat ip12 | 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-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)'
11.22.33.44
192.68.1.2
72.34.34.200
11.22.33.44
33.44.55.66

```

---

<div class="post-metadata">

**Author:** ![itkamaraj](https://community.unix.com/user_avatar/community.unix.com/itkamaraj/32/1880_2.png) [@itkamaraj](https://community.unix.com/u/itkamaraj)\
**Post date:** [May 7, 2012, 4:22am UTC](https://community.unix.com/t/grep-ip-address-from-file/309722/2 "2012-05-07T04:22:07Z")

</div>

The below regex is for identify the valid ip address.

[Regular Expression Examples](http://www.regular-expressions.info/examples.html)

```plaintext
 
\b(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]?)\b 

```

---

<div class="post-metadata">

**Author:** ![\_dagio](https://community.unix.com/user_avatar/community.unix.com/_dagio/32/1812_2.png) [@\_dagio](https://community.unix.com/u/_dagio)\
**Post date:** [May 10, 2012, 7:11am UTC](https://community.unix.com/t/grep-ip-address-from-file/309722/3 "2012-05-10T07:11:31Z")

</div>

From your first command:

```nohighlight
cat ip12 | grep '^[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}$'

```

just remove ^ and $ symbols:

```nohighlight
 #cat ip12 | grep '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'
11.22.33.44
192.68.1.2
72.34.34.200
333.444.555.666
myip1 11.22.33.44
myip2 33.44.55.66 #fine this IP should also be listed

```
