[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

count unique ip addresses in unix hosts file

Posted on 2008-11-13
5
Medium Priority
?
357 Views
Last Modified: 2013-12-26
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!!!
0
Comment
Question by:J P
5 Comments
 
LVL 19

Accepted Solution

by:
jools earned 680 total points
ID: 22948189
something like;
   cat /etc/hosts | awk '{print $1}' | sort -n | uniq | wc -l
0
 
LVL 19

Assisted Solution

by:jools
jools earned 680 total points
ID: 22948191
or :-)
  cat /etc/hosts | grep -v "^#" | grep -v "^$" | awk '{print $1}' | sort -n | uniq | wc -l
0
 
LVL 40

Assisted Solution

by:omarfarid
omarfarid earned 320 total points
ID: 22948223
try

 cat /etc/hosts | grep -v '^#' | awk '{ print $1 } ' | sort | uniq -c
0
 
LVL 6

Author Closing Comment

by:J P
ID: 31516296
thanks you all for prompt help !!!
0
 
LVL 48

Expert Comment

by:Tintin
ID: 22953061
Or a shorter solution is

awk '/^[0-9]/ {print $1}' /etc/hosts | sort -u | wc -l
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Suggested Courses
Course of the Month17 days, 17 hours left to enroll

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question