Extract IP Address from Log File

I have a log file with several IP addresses in it:-

2012-12-06 16:05:05,885 NOTICE [10674] **SNMP** Alarm was created:  (LicenseClientRejected) Client Remote Peer /125.111.64.125:2573 was 
Rejected Property=/125.111.64.125:2573, Client Remote Peer /125.111.64.125:2573 was 
Rejected **SNMP**. [New I/O server boss #6 ([id: 0x70a81b43, /125.111.64.125:2573])_11]
2012-12-06 16:16:45,739 NOTICE [10674] **SNMP** Alarm was created:  (LicenseClientRejected) Client Remote Peer /104.121.98.124:3583 was 
Rejected Property=/104.121.98.124:3583, Client Remote Peer /104.121.98.124:3583 was 
Rejected **SNMP**. [New I/O server boss #5 ([id: 0x03174ea5, /104.121.98.124:3583])_11]

How can I extract them and list the unique IP addresses using awk or sed ?

See if this gives you what you want:

grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}' log_file_name.txt | sort | uniq

OS is SunOS

uname
SunOS

And the options specified for grep is not supported:-

grep: illegal option -- E
grep: illegal option -- o
Usage: grep -hblcnsviw pattern file . . .

From your log, I have made the commands below on Solaris OS.

# cat LOG | awk '{ for ( i=1;i<=NF;i++ ) Vword[$i]++ } END { for( WORD in Vword ) print WORD }' > word.tmp
# cat word.tmp  | awk -F/ '{print $2}' | awk -F: '{print $1}'| grep -v ^$ | sort -u
104.121.98.124
125.111.64.125

Cheers,

1 Like

try also:

awk '/^[0-9.]+[.][0-9]+$/{if(!a[$0]++)print $0}' RS="[ :/\n]" infile
1 Like

OH, Ok, Then this should work using sed:

sed 's#.*/\([0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\).*#\1#g' log_file_name.txt | sort | uniq