Use builtins when possible

a call to cat can be replaced by a redirection.
Use less wc calls

Use grep -c to avoid a call to wc
Use a redirection to avoid a call to cut
Use a redirection to avoid a call to cut (second)
Use $() instead of ticks
Consistent style for loops (while and for)