Simpler Navigation for Servers and Operating Systems - Please Update Your Bookmarks
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
If you have bookmarked forums or discussion boards in Servers and Operating Systems, we suggest you check and update them as needed.
Showing results for 
Search instead for 
Did 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.
Pete Randall
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.