#!/usr/bin/env bash # # sort-lines-group-by-start — cluster lines that share their first # whitespace-separated field ($1). Keys appear in first-seen order, and lines # within a key keep their original order. It groups; it does NOT sort. # # Pass the log file as an argument; result is written to stdout: # # tools/sort-lines-group-by-start.sh filename.log # # Useful for a serial/boot log where several sources interleave and each line is # prefixed with its source (the first field): this pulls every source's lines # back together, in the order the sources first appeared, without reordering # within a source. # # input output # pci-bus: scan start pci-bus: scan start # acpi: reported PNP0303 pci-bus: 5 functions # pci-bus: 5 functions acpi: reported PNP0303 # acpi: reported PNP0501 acpi: reported PNP0501 awk '{lines[$1] = lines[$1] ? lines[$1] ORS $0 : $0; if (!seen[$1]++) order[++count] = $1} END {for (i=1; i<=count; i++) print lines[order[i]]}' "$@"