Firstly I must say that killing Oracle processes in this way is probably not the best approach to take. Secondly, if you must kill these (or any other) processes from the OS, it's always best to try "kill -15" first, to give the process a chance to handle the request gracefully. "kill -9" should only be used in extreme cases, after trying "kill -15".
If you must kill processes in this way, you could run your SQL query via sqlplus in a script, and either redirect the output to a new script, pipe the output to a loop and run the commands one by one, or put xargs to work.
Using the xargs approach, try these steps:
* It's probably easiest to put the SQL into a separate file, say staleprocesses.sql.
* Change the query to select on the PID, without prepending the "kill -9".
* You'll need to set some options to not show unnecessary information, which I'm afraid I can't help you with - haven't used sqlplus in years.
* Run the query as follows, and check that it outputs ONLY the process ids:
sqlplus username/password @staleprocesses.sql
* Once this works, run the following to actually kill the processes:
sqlplus username/password @staleprocesses.sql | xrags kill -9
I hope this helps to get you going on a solution.