M
M
mirus2014-05-30 09:45:08
Perl
mirus, 2014-05-30 09:45:08

How to remove duplicate ip addresses from a file?

there is a file with a list of addresses in it, how to remove identical ip addresses using grep or other tools so that they do not exist at all? That is, so that only unique ones remain.

$ cat access.log
212.164.35.62:80
212.164.35.62:80
212.164.35.62:443
212.164.3.95:80
212.164.4.26:80
212.164.4.26:80
212.164.4.26:443
212.164.48.174:80
212.164.49.104:80

Answer the question

In order to leave comments, you need to log in

7 answer(s)
K
Konkase, 2014-05-30
@miruss

Then like this:
cat access.log | awk '{split($0,a,":"); print a[1]}' | sort-u

V
Valentine, 2014-05-30
@vvpoloskin

cat access.log | sort | uniq

X
xotkot, 2014-05-30
@xotkot

awk -F: '{print $1}' access.log |uniq -u

A
afiskon, 2014-05-30
@afiskon

cat ip.txt | cut -d ':' -f 1 | sort -u

S
Sergei Borisov, 2014-05-30
@risik

cat access.log | awk -FS: '{printf $1 "\n"};' | sort | uniq
This is not a problem for you if you cut out the colon and the port number.
If there is a problem, then the question is, what port with such entries

212.164.35.62:80
212.164.35.62:443

should stay?

M
miruss, 2014-05-30
@miruss

thanks Konkase

#!/bin/bash

for ip in `cat ./access.log | awk '{split($0,a,":"); print a[1]}'`
  do
  dup=`cat ./access.log | awk '{split($0,a,":"); print a[1]}' | grep $ip | wc -l`
    if 
      then
        : 
      else
        echo -e "$ip" 
    fi
done

S
Semyon Voronov, 2014-06-04
@Gineaser

perl -i -ne 'print unless $a{$_}++' access.log

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question