well, now you can learn how to do it with python, without system calls and without need of 2 intermediate files...
from collections import Counter with open('first.txt', 'r') as f1: names = [ln.strip() for i, ln in enumerate(f1) if i%2] with open('fourth.txt', 'w') as f4: for key, value in sorted(Counter(names).items()): f4.write('{} {}\n'.format(key, value))