1832973 Members
2444 Online
110048 Solutions
New Discussion

Re: sort question

 
SOLVED
Go to solution
machoq1
Esteemed Contributor

sort question

I have a file with multiple node names in it. I need to know how many times does a node name appear in a file
I am looking for something to give result like
NodeA
NodeB
4 REPLIES 4
Rick Garland
Honored Contributor
Solution

Re: sort question

In addition to piping your results to the sort utility, also pipe to the uniq utility

... | sort | uniq -c

This list each item individually with a count of how many times it is listed.

machoq1
Esteemed Contributor

Re: sort question

Thanks !
Mel Burslan
Honored Contributor

Re: sort question

If this file contains nothing but the host names

you can use this:

myinfile=/path/somefilename
outfile=/path/myoutfilename

#in case multiple host names on one line we'll separate them
for h in `cat $myinfile`
do
echo $h > /tmp/temphostfile
done

for host in `cat /tmp/temphostfile|sort|uniq`
do
ct=`grep $host /tmp/hostfile |wc -l`
echo $host" "$count >> $outfile
done

if you can provide a section of your input file, you can get more sensible answers
________________________________
UNIX because I majored in cryptology...
Alan Meyer_4
Respected Contributor

Re: sort question

Given a data file containing

NodeA
NodeC
NodeB
NodeB
NodeC
NodeA
NodeA
NodeA
NodeA
NodeC
NodeC

running the script

sort node.dat|uniq |while read node;do
echo "$node = \c"
echo "`grep $node node.dat |wc -l`"
done


produces

NodeA = 5
NodeB = 2
NodeC = 4
" I may not be certified, but I am certifiable... "