Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

grep ip address from file

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 05-07-2012   -   Original Discussion by anil510
anil510 anil510 is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 16 March 2015, 2:50 AM EDT
Posts: 100
Thanks: 47
Thanked 2 Times in 2 Posts
grep ip address from file

I have a file


Code:
[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

Code:
[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


Code:
[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

Sponsored Links
    #2  
Old Unix and Linux 05-07-2012   -   Original Discussion by anil510
itkamaraj's Unix or Linux Image
itkamaraj itkamaraj is offline Forum Advisor  
Registered User
 
Join Date: Apr 2010
Last Activity: 5 November 2017, 9:59 PM EST
Posts: 3,148
Thanks: 45
Thanked 698 Times in 674 Posts
The below regex is for identify the valid ip address.

Regular Expression Examples


Code:
 
\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

Sponsored Links
    #3  
Old Unix and Linux 05-10-2012   -   Original Discussion by anil510
@dagio's Unix or Linux Image
@dagio @dagio is offline
Registered User
 
Join Date: Feb 2010
Last Activity: 1 November 2015, 8:07 AM EST
Posts: 77
Thanks: 3
Thanked 5 Times in 4 Posts
From your first command:

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

just remove ^ and $ symbols:


Code:
 #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

Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
grep search for http address in a file. colmbell Shell Programming and Scripting 5 07-26-2010 11:34 AM
how to grep ip address ? raghu_r77 Shell Programming and Scripting 6 11-05-2009 10:11 AM
grep ip address rein Shell Programming and Scripting 2 05-22-2009 02:25 AM
Grep Ip address wfleenor UNIX for Dummies Questions & Answers 6 03-07-2009 09:09 AM
How to grep dhcp ip address chongkls77 Shell Programming and Scripting 4 03-28-2007 10:27 AM



All times are GMT -4. The time now is 01:43 AM.