Showing results for 
Search instead for 
Do you mean 

Script help

Go to Solution
Occasional Contributor

Script help


I would like to know how to search through the host table to match computers on a particular subnet, then to give the total number matched at the end using a while loop or something (my scripting knowledge is very limited).

Any ideas would be greatly appreciated.
Outstanding Contributor

Re: Script help

How about a simple grep/wc?

grep /etc/hosts |wc -l


Honored Contributor

Re: Script help

This could get a bit more complex depending on how your subnets are configured. Generally, you can do something like this, however:

grep -c ^WWW.XXX.YYY. /etc/hosts

-c returns the count instead of the matches (saves the piping of grep to wc)

^ only matches if the address is at the beginning of the line (avoids grabbing commented lines)
Jeff Traigle
Occasional Contributor

Re: Script help

Thanks guys! That's what I needed.

And thanks Jeff for the detailed explanation. It helps the newbies like myself.