Sort a section of a file

In Python:

#!/usr/bin/python3

with open("file.txt", "r") as ins:
    lines = []
    for line in ins:
        if line.startswith((" ", "\t")):
            lines.append(line)
        else:
            lines.sort()
            print(*lines, end = "", sep = "")
            print(line, end = "")
            lines = []
    lines.sort()
    print(*lines, end = "", sep = "")

This sorts all the sections (separately), not only those between two specific lines.


For the fun, here is a way to sort a single section using ex:

ex file <<%
/HUT
+1,/HUT/-1!sort
w file.sorted
q
%

$ awk 'BEGIN { OFS="\t"; s=0 } /^[^[:blank:]]/ { print ++s "\b", $0; next } { print s, $0 }' file | sort -n | cut -f 2-
    0ce  Handle Bars
    0cf  Front Brake
    0d0  Rear Brake
HUT 03  VR Controls
    000  Unidentified
    001  Belt
    002  Body Suit
    003  Flexor
    004  Glove
    005  Head Tracker
    006  Head Mounted Display
    007  Hand Tracker
    008  Oculometer
    009  Vest
    00a  Animatronic Device
    020  Stereo Enable
    021  Display Enable
HUT 04  Sport Controls
    000  Unidentified
    001  Baseball Bat
    002  Golf Club

This uses awk to add a number (and a tab separator) in front of each line corresponding to the section that this line is in. For section headers, we add a number followed by a backspace character (only because backspace sorts before tabs). Then we simply sort the resulting data on these numbers before removing them and the added tab separators.

Section headers are detected by looking for non-blank characters at the start of the line.

Tags:

Sort