Grep for ip address only from a file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Grep for ip address only from a file
# 1  
Old 01-05-2016
Grep for ip address only from a file

Facing issues in grepping only the IP Address from a file i have tried the below and it was of not much help


Code:
awk -F"[<>]" '/(/ { print $3 }'


Code:
awk -F"</*(>" '/ip/ { print $2 }'


Code:
grep "ip" file1|cut -f2 -d"<"|cut -f2 -d">"


Code:
grep "(" file1 |cut -f2 -d"<"|cut -f2 -d">"


Code:
grep -e '^[1-9][0-9]\.[1-9][0-9]\.[1-9][0-9][]0-9]\.[1-9][0-9][]0-9]' file1


Code:
egrep "10.*" file1


the contents of the file is



Code:
/exp/abc110.20.20.21(rw,nohide,sync,insecure,root_squash,no_subtree_check)10.25.22.21(rw,nohide,sync,insecure,root_squash,no_subtree_check)10.25.22.26(rw,nohide,sync,insecure,root_squash,no_subtree_check)
/exp/ad110.24.18.30(rw,nohide,sync,insecure,root_squash,no_subtree_check)10.56.34.33(rw,nohide,sync,insecure,root_squash,no_subtree_check)
/exp/ab110.56.22.23(ro,sync,nohide,insecure,no_subtree_check)10.56.22.27(ro,sync,nohide,insecure,no_subtree_check)
/exp/be110.24.89.60(ro,sync,nohide,insecure,no_subtree_check)10.56.22.20(ro,sync,nohide,insecure,no_subtree_check)
/exp/dc110.27.23.10(ro,sync,nohide,insecure,no_subtree_check)10.56.22.26(ro,sync,nohide,insecure,no_subtree_check)


grep for ip address alone from a file

---------- Post updated at 03:33 PM ---------- Previous update was at 03:27 PM ----------

i have even tried these

Code:
egrep -o '[0-9]+[.][0-9]+[.][0-9]+[.][0-9]+/[0-9]+' file1

Code:
awk '{match($0, /[0-9]+\.[0-9]+\.[0-9]+\.*[0-9]+\/[0-9]+/); print substr($0, RSTART, RLENGTH)}'  file1

still the output is not there .. i need the output of only IP's.

Moderator's Comments:
Mod Comment edit by bakunin: please use CODE-tags as required by the forum rules? Thank you!
# 2  
Old 01-05-2016
How about
Code:
grep -o ")[0-9.]*(" file | tr -d ')('
10.25.22.21
10.25.22.26
10.56.34.33
10.56.22.27
10.56.22.20
10.56.22.26

# 3  
Old 01-05-2016
First: tools like grep will not help you because they filter certain lines (in which a certain regexp is found) only without modifying them. If you want only the IP addresses alone with no other text surrounding you need to some text-modifying tool like sed (or awk, perl, ...).

Having said this: you first need to clarify what an "IP-Address" (per your definition) is. For instance: an unsigned integer 1-256, followed by a triplett of unsigned integers 0-256, preceeded by a dot:

Code:
[1-256]  .[0-256]  .[0-256]  .[0-256]

Now, this definition would cover an "IP-Address" like "1.0.0.0", which would not be a valid address of a host (it would be the network address of the 1/8 net).

On the other hand, changing the definition to cover this, like:

Code:
[1-256]  .[0-256]  .[0-256]  .[1-256]

would invalidate an address like 10.1.1.0, which would be a valid address in the network 10.1/16.

So, the question is: which level of "rule-adherence" would you like to build into your regexp?

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 4  
Old 01-05-2016
grep for /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/

Code:
cat <filename> |grep (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})


Last edited by Don Cragun; 01-13-2016 at 04:08 AM.. Reason: Add CODE and ICODE tags.
This User Gave Thanks to girijajoshi For This Post:
# 5  
Old 01-05-2016
Code:
perl -lne 'print if s/.*?(\d+\.\d+\.\d+\.\d+)\D*/ $1/g' file1

Your egrep attempt was close:
Code:
egrep -o '[0-9]+[.][0-9]+[.][0-9]+[.][0-9]+' file1


Last edited by MadeInGermany; 01-05-2016 at 01:39 PM..
This User Gave Thanks to MadeInGermany For This Post:
# 6  
Old 01-05-2016
IP groups for each line:

Code:
perl -nle '@ip=/(\d+\.\d+\.\d+\.\d+)/g and print "@ip"' file1

Code:
110.20.20.21 10.25.22.21 10.25.22.26
110.24.18.30 10.56.34.33
110.56.22.23 10.56.22.27
110.24.89.60 10.56.22.20
110.27.23.10 10.56.22.26

Each IP in its own line:

perl -nle 'while(/(\d+\.\d+\.\d+\.\d+)/g){print $1}' file1
Code:
110.20.20.21
10.25.22.21
10.25.22.26
110.24.18.30
10.56.34.33
110.56.22.23
10.56.22.27
110.24.89.60
10.56.22.20
110.27.23.10
10.56.22.26

All the IPs in line:

perl -nle 'push @ip, /(\d+\.\d+\.\d+\.\d+)/g; END{print "@ip"}' file1
Code:
110.20.20.21 10.25.22.21 10.25.22.26 110.24.18.30 10.56.34.33 110.56.22.23 10.56.22.27 110.24.89.60 10.56.22.20 110.27.23.10 10.56.22.26

These 2 Users Gave Thanks to Aia For This Post:
# 7  
Old 01-07-2016
Quote:
Originally Posted by satishcarya
[..]
i have even tried these

Code:
egrep -o '[0-9]+[.][0-9]+[.][0-9]+[.][0-9]+/[0-9]+' file1

Code:
awk '{match($0, /[0-9]+\.[0-9]+\.[0-9]+\.*[0-9]+\/[0-9]+/); print substr($0, RSTART, RLENGTH)}'  file1

still the output is not there .. i need the output of only IP's.
[..]
From these I gather that besides ip addresses you also want to collect IP CIDR ranges. If so, you were close:

Code:
egrep -o '[0-9]+[.][0-9]+[.][0-9]+[.][0-9]+(/[0-9]+)?' file1

or

Code:
grep -Eo '([0-9]+[.]){3}[0-9]+(/[0-9]+)?' file1

This User Gave Thanks to Scrutinizer For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep -w ip address from a file isnt working need help

I have a file "file1" that contains several ip address , and the "file2" contains several records , each line in file2 contains somewhere the ip address that i am searching in the file1 I use the unix command grep -w for i in `cat file1` do grep -w "$i" file2 >> file3 done ... (9 Replies)
Discussion started by: knijjar
9 Replies

2. Shell Programming and Scripting

GREP mac address

Hi, mac.txt My mac address is <Mac Address>. How can i replace <Mac Address> with the actual of my computer? I try to GREP command as below but i am unable to grep it to replace just <Mac Address>. ifconfig eth0 | grep -o -E '(]{1,2}:){5}]{1,2}' Million in Advance. Please use... (7 Replies)
Discussion started by: derrickyee81
7 Replies

3. Shell Programming and Scripting

Grep IP address form a text file along with the subnet

I have an input file: class 1 3 5 10.10.10..0/23 hicks jimmy class 3 10.12.10.0/22 mike class.019283 10.10.15.10/20 henry gym.847585 45 192.168.10.0/22 nancy jim steve maya The output should look like this: 10.10.10..0/23 10.12.10.0/22 10.10.15.10/20 192.168.10.0/22 I have the... (3 Replies)
Discussion started by: e_mikey_2000
3 Replies

4. UNIX for Dummies Questions & Answers

Grep exact IP address from file

I have a file with a lot of IP addresses in it named "address.list". address.list looks something like this: 10.77.50.11 10.77.50.110 10.77.50.111 a bunch more addresses For every IP address I need to grep another file to see if the IP address is in the other file: for x in `cat... (5 Replies)
Discussion started by: squoggle
5 Replies

5. Shell Programming and Scripting

grep ip address from file

I have a file $ 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 $ cat ip12 | grep '^\{1,3\}\.\{1,3\}\.\{1,3\}\.\{1,3\}$' 11.22.33.44 192.68.1.2 (2 Replies)
Discussion started by: anil510
2 Replies

6. Shell Programming and Scripting

grep search for http address in a file.

Hi I have downloaded a HTM file from the web. What I want to do is perform a grep search of that file, searching for all strings where 'http' is present within the file, but only contains the word 'cache' within the string. I've includeda sample file, which I'm trying to extract the above... (5 Replies)
Discussion started by: colmbell
5 Replies

7. Shell Programming and Scripting

how to grep ip address ?

hi, Pls advice me, how to grep only the ip address from the following output: <ip>10.4.65.67</ip> <TLS_RSA_EXPORT1024_WITH_DES_CBC_SHA>false</TLS_RSA_EXPORT1024_WITH_DES_CBC_SHA> <TLS_RSA_EXPORT1024_WITH_RC4_56_SHA>false</TLS_RSA_EXPORT1024_WITH_RC4_56_SHA> <ip>10.4.65.67</ip>... (6 Replies)
Discussion started by: raghu_r77
6 Replies

8. Shell Programming and Scripting

grep ip address

I need to find out whether a line in a text file contains an ip address or a hostname. How can I do this? For example: 123.2.34.55 host1 host2.domain.com host3.intra.domain.com Then I want as result a list with ip addresses and hostnames. (2 Replies)
Discussion started by: rein
2 Replies

9. UNIX for Dummies Questions & Answers

Grep Ip address

I'm looking a Grep or egrep statement that would allow me to exclude the a range of ip address via the last octet example egrep -v "*.*.*.(54|61)" from a syslog file.... would this work? (6 Replies)
Discussion started by: wfleenor
6 Replies

10. Shell Programming and Scripting

How to grep dhcp ip address

Hi, I have a script that my operators use as a login profile. As they need to export their display in order to access the GUI of the data protector program in HPUX machine. Anyone can advise how I can grep (eg. who -r) the dynamically assigned IP address and automatically put it as a variable... (4 Replies)
Discussion started by: chongkls77
4 Replies
Login or Register to Ask a Question