save 1st 1k and last 3k lines of cmsRun stdout instead of last 1MB, fixes #4277

Can we please not call shell commands if not necessary? A quick search on stack overflow turns up:

num_lines = sum(1 for line in open('myfile.txt'))

And two similar commands can be used to build your desired output file.