bash : Count number of recurrence of lines

Say, we have a file or data that has many duplicate rows or entries and we want to find how many time each one has repeated and maybe want to know which is repeated most of the time. Here is an elegant script that can do that in single line.

sort input.file | uniq -c | sort -n -r

Explanation:
First sort will sort the records in the file. Then uniq -c will count how many times each record is duplicated. And finally sort -n -r will sort the output of uniq -c in reverse order giving us the records that repeated most often to the least often.
Continue reading

How to find number of CPUs on unix system

Newer processors are multi-core and could have hyper-threading enabled. So there are time when user may need to know how many cores (virtual) processors are available and how many Physical (real) processors are installed. CPU information can be retrieved via /usr/sbin/psrinfo on SunOS while it is available in /proc/cpuinfo on Linux. /proc/cpuinfo on Linux contains information about each available core. Following command combinations can be used to retrieve the number of CPUs.

OS Physical Processors Cores available
SunOS /usr/sbin/psrinfo -p /usr/sbin/psrinfo | wc -l
Linux grep “^physical id” /proc/cpuinfo | awk ‘{print $NF}’ | sort -u | wc -l grep processor /proc/cpuinfo | wc -l
IRIX hinv | grep -i processor | head -n1 | cut -d’ ‘ -f1

Update: Thanks Matias for tip on IRIX.