Well, your immediate problem is that
echo is not a command that can accept input from stdin, so you can't use it in the middle of a pipe chain like that. Oh, and there's an embedded
wc command without any input following that echo.
I see a couple of other errors and weak points, such as the "&j" in
grep (I think you want "$j"), that you could probably combine the two sed commands into one, and that
$(..) is highly recommended over `..`. Also, your code would be much easier to read and debug if you'd use a decent indentation scheme*. But I'm too tired right now to go into any more detail.
*Edit: I just noticed you're using quote tags instead of the proper
[code][/code] tags. quote tags don't preserve formatting the way code tags do. So apologies if your code originally had indentations.