... or using $() instead of the backticks, easier to read :)
–
slhck♦Jun 7 '11 at 20:48

4

The assumption of 4-5 digits is strange; most ps implementations don't zero-pad the left of pids, and pid usage can wrap around. Some OSes even actually use more than 16-bits of pid space.
–
Phil PJun 7 '11 at 21:50

The | operator feeds the "standard output" (stdout) from the left to the "standard input" of the right; standard input, or stdin, is equivalent to "what I read as though from the user if they type into me". The kill(1) command kills the process ids provided on its command-line, not on its stdin. So inserting xargs before kill would help:

... | xargs kill

because xargs(1) takes its stdin and batches it up into chunks, to repeatedly invoke the supplied command (here, "kill") with command-lines made up of those chunks.

The > operator is used to redirect stdout to a file, so you've created a new file in the current directory, which has the filename "kill" and contents of the pids.

The pattern \d is not a part of either the "standard" or "extended" regular expression languages; instead, it was introduced by Perl (I believe) and is in many later variants of the regular expression language. PCRE, for Perl Compatible Regular Expressions, provides a library which was instrumental in spreading the extended syntax to many other tools and languages, but is certainly not the only implementation today. There are websites which will provide matrix comparisons of the different regexp languages for you.

PCRE ships with pcregrep(1) which will understand \d for you; also, modern GNU grep has the -P option, which will use PCRE for regexps.

You're here after the concept called "command substitution", where the stdout of the command is used directly in the command-line; the modern syntax for this is $(...), although you'll also see backticks used, `...` but that's historical and doesn't nest well. So: