5 # Sort an input file that consists of lines like this
7 # var1=23 other=14 ditto=23 fred=2
9 # such that each output line is sorted WRT to the number. Order
10 # of output lines does not change. Resolve collisions using the
13 # fred=2 other=14 ditto=23 var1=23
15 # Lines may be up to several kilobytes in length and contain
16 # zillions of variables.
18 # This implementation:
19 # - Reads stdin, writes stdout
20 # - Uses any amount of whitespace to separate fields
21 # - Allows signed numbers
22 # - Treats illegally formatted fields as field=0
23 # - Outputs the sorted fields with exactly one space between them
24 # - Handles blank input lines correctly
30 prog
= re
.compile('^(.*)=([-+]?[0-9]+)')
31 def makekey(item
, prog
=prog
):
32 match
= prog
.match(item
)
34 var
, num
= match
.groups()
37 # Bad input -- pretend it's a var with value 0
39 for line
in sys
.stdin
:
40 items
= sorted(makekey(item
) for item
in line
.split())
41 for num
, var
in items
:
42 print "%s=%s" % (var
, num
),