count unique ip addresses in unix hosts file

hi unix gurus!!!
im stumped on a simple solution for this.
for an/etc/hosts file that contains entries in format:
IPAddress <space> Hostname <newline>

Example:
127.0.0.1 localhost
169.0.0.69 webserver

what would be the simplest way to COUNT the number of UNIQUE IPAddresses in a single piped command?

cheers!!!
LVL 6
J PAsked:
Who is Participating?
 
joolsConnect With a Mentor Commented:
something like;
   cat /etc/hosts | awk '{print $1}' | sort -n | uniq | wc -l
0
 
joolsConnect With a Mentor Commented:
or :-)
  cat /etc/hosts | grep -v "^#" | grep -v "^$" | awk '{print $1}' | sort -n | uniq | wc -l
0
 
omarfaridConnect With a Mentor Commented:
try

 cat /etc/hosts | grep -v '^#' | awk '{ print $1 } ' | sort | uniq -c
0
 
J PAuthor Commented:
thanks you all for prompt help !!!
0
 
TintinCommented:
Or a shorter solution is

awk '/^[0-9]/ {print $1}' /etc/hosts | sort -u | wc -l
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.