[Solved] How to add line numbers to a printed table


Assuming the content is in file “txt”, following code should work in python. (though it does not preserve the whitespaces)

with open ("txt") as f:
    for line in sorted(
        [line.rstrip().split() for line in f],
        key=lambda x : int(x[1])
    ):
        print(" ".join(line))

Simply, in cmdline you can do something like below, while preserving white spaces

sort -k 2 -g txt

7

solved How to add line numbers to a printed table